我在其中有一个 ng-repeat 和一个自定义指令,我试图将 ng-repeat 中的“item”变量传递给它们:
<li ng-repeat="item in list">
<div custom-directive custom-data="item"></div>
</li>
想象一个虚拟指令:
angular.module('someModule').directive('customDirective', function() {
restrict: 'A',
scope: {customData: '@'},
link: function(scope) {
console.log(scope.customData);
}
});
- 如果我使用
custom-data="item"
然后在指令中使用 customData 等于字符串“item”。 - 如果我使用
custom-data="{{item}}"
然后 customData 在 指令是item
对象的字符串化表示,并且 对其中其他对象的引用丢失。
问题是:如何将对象形式的 item
传递给我的指令?
最佳答案
在你的范围分配中将'@'更改为'='然后它将起作用
angular.module('someModule').directive('customDirective', function() {
restrict: 'A',
scope: {customData: '='},
link: function(scope) {
console.log(scope.customData);
}
});
关于javascript - 将 ng-repeat 变量传递给自定义指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30458982/