【发布时间】:2015-06-22 19:47:10
【问题描述】:
在 Angular 中创建自定义指令时,我有一个担忧。 当我使用链接函数时,我不确定使用 attrs 或范围访问属性时的真正区别是什么。 以这段代码为例:
myApp.directive('someDirective', function() {
return {
restrict: 'E',
replace: true,
scope: {
title: '=title'
},
template: '<img/>',
link: function(scope, element, attrs) {
if (scope.title) {
// do something here
}
if (attrs.title){
// do something here
}
},
}
根据我的观察,从 attrs 和按范围访问“title”属性具有类似的效果。真正的区别是什么?
【问题讨论】:
标签: javascript angularjs angularjs-directive