[英]AngularJS directive scope not resolved (“attr name is not defined” error)
Directive code 指令代码
mymodule.directive('eicon', function(){
return {
restrict: 'E',
scope: {
attr: '='
},
template: "test " + attr.name
}
});
Html HTML
<tr ng-repeat="e in entities">
<td><eicon attr="e"></eicon></td>
</tr>
I've this error: ReferenceError: attr is not defined
. 我有这个错误:
ReferenceError: attr is not defined
。 What's wrong? 怎么了?
Since you are declaring isolated scope property attr
you should be able to access scope.attr
in template like this: 由于您正在声明隔离的范围属性
attr
您应该能够在模板中访问scope.attr
,如下所示:
mymodule.directive('eicon', function(){
return {
restrict: 'E',
scope: {
attr: '='
},
template: "test {{attr.name}}"
}
});
Demo: http://plnkr.co/edit/YZ0aPqhkMlIIwmrkKK2v?p=preview 演示: http : //plnkr.co/edit/YZ0aPqhkMlIIwmrkKK2v?p=preview
attr
is accessible in scope, so you can access scope.attr
in your controller or linking phase, or {{attr}}
in templates. attr
在范围内是可访问的,因此您可以在控制器或链接阶段或模板中的{{attr}}
中访问scope.attr
。 A simple solution is to change your template to 一个简单的解决方案是将模板更改为
mymodule.directive('eicon', function(){
return {
restrict: 'E',
scope: {
attr: '='
},
template: "test {{attr.name}}",
link: function (scope, element, attrs) {
console.log(scope.attr);
},
controller: function (scope) {
console.log(scope.attr);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.