我正在尝试显示两个列表。第一个是对象数组的循环。添加和删除该阵列中的项目按计划工作。嵌套列表从工厂中提取数据,工厂附加到第一个循环中的每个项目,并根据原始循环中的ID对其进行过滤。
我无法在嵌套元素上触发$ watch函数。有什么东西我不见了吗?是否有另一种方法在Angular最佳实践方面会更好?
app.directive('relatedItems', function ($compile) {
return {
restrict: "E",
replace: true,
scope: {
variations: '='
},
template: "<ul><variation-item ng-repeat='variation in variations' variation='variation'></variation-item></ul>",
}
})
app.directive('variationItem', function ($compile) {
return {
restrict: "E",
replace:true,
scope: {
variation: '=',
},
template: '<li ng-repeat="actor in actors"></li>',
link: function (scope, element, attrs) {
scope.$watch('variation.play.actors', function(){
scope.actors = [];
for(index in scope.variation.play.actors){
if(index.endsWith(\"-Var\" + scope.variation.id)){
scope.actors.push(scope.variation.play.actors[index]);
}
}
});
}
}
})