Ben*_*307 2 angularjs angularjs-directive ng-repeat angularjs-scope angularjs-ng-repeat
我正在使用angularjs并且需要找到它的手表ng-repeat,因为我需要ng-repeat从特定点停止工作.我的代码:
<ul>
<li ng-repeat="item in items">
<div>{{item.name}}</div>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我需要找到的观察者ng-repeat.如果我去scope.$parent.$$watchers[x]并执行splice手表被删除,但我怎么能找到具体的手表?
无法找到手表(请参阅下面的说明),但您可以使用以下指令实现您的目标
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;
}
);
Run Code Online (Sandbox Code Playgroud)
它的用法是
<ul>
<li repeat-static ng-repeat="item in items">
{{ item }}
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
请参阅http://plnkr.co/k9BTSk作为工作示例.
理性的背后是angular directive ng-repeat指令使用内部函数$ watchCollection来添加一个自己创建的监听项目对象的监听器.由于侦听器是在进程中创建的函数,并且不作为参考保留在任何位置,因此没有好的方法来正确识别要从$$观察器列表中删除的函数.
但是,可以通过使用属性指令将新范围强制进入ng-repeat,这样,ng-repeat添加的$$观察者将与父范围隔离.因此,我们获得了对范围的完全控制.$$观察者.在ng-repeat使用填充值的函数后立即,可以安全地删除$$观察者.该解决方案使用hassassin清理$$观察者列表的想法.
| 归档时间: |
|
| 查看次数: |
4770 次 |
| 最近记录: |