如何观察AngularDart集合的元素? [英] How do I watch elements of an AngularDart collection?

查看:113
本文介绍了如何观察AngularDart集合的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型:

class WordList {
  List<Word> words = [];
}

它是通过依赖注入到我的一个视图创建的。

It's created via dependency injection into one of my views.

@NgController(
    selector: '[list-ctrl]',
    publishAs: 'ctrl'
)
class ListCtrl {
  WordList wordList;
  Scope scope;

  ListCtrl(this.router, this.wordList, this.scope) {
    scope.$watchCollection("", onChange );
  }



我想从该列表修改项目时运行一些逻辑。

I'd like to run some logic whenever an item is modified from that list. How do I accomplish this?

我认为键位于$ watchCollection中,但是我不知道要作为一个监视表达式传递什么。

I believe the key is in the $watchCollection, but I can't figure out what to pass as a watch expression. "ctrl.wordList.words" will tell me when items are added/removed, but not changed.

推荐答案

$ watchCollection ,因为你指出只能监视 List 中的更改,而不是列表中项目的更改。这样做的原因是,观察每个对象将具有爆炸性数量的属性。

$watchCollection as you point out can only watch for changes in the List not for changes in the items of the list. The reason for this is that watching each object would have explosive number of properties.

scope.$watch(() => wordList, onChange);

您可以实现 onChange 方式,它将创建进一步的手表每个新项目,以及取消注册从集合中删除项目的手表。

You could implement the onChange method in a way that it would create further watches on each new item as well as deregister the watch on item removal form the collection.

这篇关于如何观察AngularDart集合的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆