我正在努力寻找正确的 Angular 方式来观看任意 ng-repeat 集合。 目标只是当 ng-repeat 的内容发生变化时接收新的列表内容。 (当然不知道集合名称)。
假设我添加了一个 collection-update
指令,如下所示:
<ul collection-update="updateCounter($list)">
<li ng-repeat="item in items|uppercase">{{ item.title }}</li>
</ul>
我希望在每次列表更改时收到当前的 ng-repeat 项目(已过滤)。
我可以依赖播音员/通知者模式,例如 http://plnkr.co/edit/6IrvzD9PcbskWq3C0qPr?p=preview但也许有更简单的东西,可以简单地“观看”集合。
最后,目标是为 http://blog.revolunet.com/angular-carousel 添加“指标”支持。
谢谢:)
最佳答案
您正在寻找$watch
,请参阅http://docs.angularjs.org/api/ng.$rootScope.Scope了解更多信息。
来自文档:
$watch(watchExpression, listener, objectEquality)
Registers a listener callback to be executed whenever the watchExpression changes.
例如,您可以在 Controller 中执行以下操作:
scope.$watch('items', function(newValue, oldValue) {
updateCounter(newValue);
});
关于javascript - 观看任意 ng-repeat 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16269029/