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

查看:21
本文介绍了如何查看 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 中,但我不知道将什么作为监视表达式传递.ctrl.wordList.words"会告诉我何时添加/删除项目,但未更改.

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天全站免登陆