我有一个模特:
class WordList {
List<Word> words = [];
}
Run Code Online (Sandbox Code Playgroud)
它是通过依赖注入我的一个视图创建的.
@NgController(
selector: '[list-ctrl]',
publishAs: 'ctrl'
)
class ListCtrl {
WordList wordList;
Scope scope;
ListCtrl(this.router, this.wordList, this.scope) {
scope.$watchCollection("", onChange );
}
Run Code Online (Sandbox Code Playgroud)
每当从该列表修改项目时,我都想运行一些逻辑.我该如何做到这一点?
我相信关键是在$ watchCollection中,但我无法弄清楚要作为监视表达式传递什么."ctrl.wordList.words"会告诉我何时添加/删除项目,但不会更改.
$watchCollection正如您所指出的那样,只能观察List列表中项目的更改中的更改.这样做的原因是观察每个对象会有爆炸性的属性.
scope.$watch(() => wordList, onChange);
Run Code Online (Sandbox Code Playgroud)
您可以onChange通过以下方式实现该方法,即在每个新项目上创建更多监视,以及从集合中删除项目移除时监视.
| 归档时间: |
|
| 查看次数: |
1376 次 |
| 最近记录: |