指令代码
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>
我遇到此错误:ReferenceError: attr is not defined。怎么了?
ReferenceError: attr is not defined
attr在作用域中是可访问的,因此您可以scope.attr在控制器或链接阶段或{{attr}}模板中进行访问。一个简单的解决方案是将模板更改为
attr
scope.attr
{{attr}}
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); } } });