我使用的是angularjs,需要找到ng-repeat的手表,因为我需要angularjs从特定点停止工作。这是我的代码:
<ul>
<li ng-repeat="item in items">
<div>{{item.name}}</div>
</li>
</ul>我要找到ng-repeat的守望者。如果我转到scope.$parent.$$watchers[x]并执行splice,该监视器将被移除,但如何找到特定的监视器?
发布于 2014-03-21 00:14:29
不可能找到手表(参见下面的解释),但是您可以使用以下指令来实现您想要的结果
app.directive('repeatStatic',
function factory() {
var directiveDefinitionObject = {
restrict : 'A',
scope : true, // this isolates the scope from the parent
priority : 1001, // ng-repeat has 1000
compile : function compile() {
return {
pre : function preLink(tElement, tAttrs) {
},
post : function postLink(scope, iElement, iAttrs, controller,
transcludeFn) {
scope.$apply(); // make the $watcher work at least once
scope.$$watchers = null; // remove the $watchers
},
};
}
};
return directiveDefinitionObject;
}
);它的用法是
<ul>
<li repeat-static ng-repeat="item in items">
{{ item }}
</li>
</ul>请参阅http://plnkr.co/k9BTSk作为工作示例。
背后的原因是angular指令ng-repeat指令使用内部函数$watchCollection来添加一个自创建的监听器来监视items对象。由于侦听器是在处理过程中创建的函数,并且没有保留在任何地方作为引用,因此没有好的方法来正确地确定要从$$watchers列表中删除哪个函数。
然而,通过使用指令,可以将新的作用域强制到ng-中,这样,由ng-添加的$$watchers就与父作用域隔离了。因此,我们获得了对作用域的完全控制。在ng-repeat使用填充值的函数之后,可以安全地删除$$watchers。这个解决方案使用了hassassin的清理$$watchers列表的想法。
发布于 2014-03-16 22:22:13
我有一个角度的叉子,可以让你将手表保留在$$watchers中,但大多数时候会跳过它。与编写编译HTML的自定义指令不同,它允许您在内部使用普通的角度模板,不同之处在于一旦内部完全呈现,手表将不再被检查。
除非你真的需要额外的性能,否则不要使用它,因为它可能会有令人惊讶的行为。
这就是它:
https://github.com/r3m0t/angular.js/tree/digest_limit
下面是您可以对其使用的指令(new-once):
https://gist.github.com/r3m0t/9271790
如果你想让页面永远不更新,你可以使用new-once=1,如果你想让它有时更新,你可以使用new-once=updateCount,然后在你的控制器中运行$scope.updateCount++;来触发更新。
更多信息:https://github.com/Pasvaz/bindonce/issues/42#issuecomment-36354087
发布于 2014-03-19 00:55:45
我过去处理这个问题的方法是,我创建了一个自定义指令,该指令复制内置ngRepeat指令的逻辑,但从不设置手表。您可以看到此here的一个示例,它是从1.1.5版的ngRepeat代码创建的。
另一种方法,正如您所提到的,是从作用域的$$watchers中删除它,这有点奇怪,因为它访问私有变量。
具体方法是在repeat上创建一个自定义指令,以删除作为repeat的监视。我创建了一个执行此操作的fiddle。它基本上只是在repeat的最后一个元素上清除了父监视(即data上的监视)。
if (scope.$last) {
// Parent should only be the ng-repeat parent with the main watch
scope.$parent.$$watchers = null;
}可以根据您的特定情况对其进行修改。
希望这能有所帮助!
https://stackoverflow.com/questions/22331185
复制相似问题