admin管理员组文章数量:1429844
Directive code
mymodule.directive('eicon', function(){
return {
restrict: 'E',
scope: {
attr: '='
},
template: "test " + attr.name
}
});
Html
<tr ng-repeat="e in entities">
<td><eicon attr="e"></eicon></td>
</tr>
I've this error: ReferenceError: attr is not defined
. What's wrong?
Directive code
mymodule.directive('eicon', function(){
return {
restrict: 'E',
scope: {
attr: '='
},
template: "test " + attr.name
}
});
Html
<tr ng-repeat="e in entities">
<td><eicon attr="e"></eicon></td>
</tr>
I've this error: ReferenceError: attr is not defined
. What's wrong?
2 Answers
Reset to default 3Since you are declaring isolated scope property attr
you should be able to access scope.attr
in template like this:
mymodule.directive('eicon', function(){
return {
restrict: 'E',
scope: {
attr: '='
},
template: "test {{attr.name}}"
}
});
Demo: 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. 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);
}
}
});
版权声明:本文标题:javascript - AngularJS directive scope not resolved ("attr name is not defined" error) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745466812a2659566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论