我写了下面的指令来在 ngRepeat
时调用一个函数元素在 UI 上呈现。
指令
directives.directive('repeatDone', function() {
return {
restrict: 'A',
require: 'ngRepeat',
link: function(scope, element, attrs, ngModel) {
if (scope.$last) {
scope.$eval(attrs.repeatDone);
}
}
};
});
但它正在给
$compile
错误。如果我删除需要部分,它工作正常。
为什么 AngularJS 不能接受“要求:'ngRepeat'”?
帮助将不胜感激。
最佳答案
require
用于访问另一个指令的 Controller 。但是ng-repeat
没有 Controller 。看看source为 ng-repeat
, Controller 这个词甚至没有出现在代码中。 documentation也没有提到 ng-repeat
的 Controller .
通常,当您使用 require
时这是因为您想调用所需 Controller 的功能。在指令的链接函数中,您添加了参数 ngModel
-- 如果 Controller 存在,它将填充 Controller 。但你从不使用这个 ngModel
代码中的参数。不太清楚为什么需要使用 require
在这种情况下。
编辑 :
在进一步审查中,也许您正试图要求 ng-repeat
因为您的 repeat-done
指令在 ng-repeat
的上下文之外不起作用.如果这是原因,另一种解决方案可能是遍历 DOM,查看 repeat-done
的父元素。指令以查看它们是否具有“ng-repeat”属性。如果未找到,您的指令可能会引发错误。但这似乎是更多的代码,而投资返回却很少......
关于angularjs - 指令不能 '' 需要 :ngRepeat',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386289/