【发布时间】:2021-06-20 06:52:11
【问题描述】:
我有以下指令:
.directive("testDir",function(){
var templateCreation = 0;
return {
template : function(){
return "<div id='myTestDirId-'"+(++templateCreation)+">Test dir : "+templateCreation+"</div>";
},
scope : {},
link: function (scope){}
}
})
我的目标是每次创建指令时都有一个唯一的 id。如果该指令包含在 ng-repeat 中,那将不起作用。例如:
<test-dir></test-dir>
<test-dir></test-dir>
<div ng-repeat="r in [1,2,3]">
<test-dir></test-dir>
</div>
会导致
Test dir : 1
Test dir : 2
Test dir : 3 -> id attribute = myTestDirId-3
Test dir : 3 -> id attribute = myTestDirId-3
Test dir : 3 -> id attribute = myTestDirId-3
但我想要这个:
Test dir : 1
Test dir : 2
Test dir : 3 -> id attribute = myTestDirId-3
Test dir : 4 -> id attribute = myTestDirId-4
Test dir : 5 -> id attribute = myTestDirId-5
知道如何强制 ng-repeat 再次构建指令吗?
【问题讨论】:
标签: javascript angularjs angularjs-ng-repeat