How do I watch elements of an AngularDart collection?

左心房为你撑大大i 提交于 2019-11-30 17:48:44

问题


I have a model:

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?

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.


回答1:


$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);

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.



来源:https://stackoverflow.com/questions/20955205/how-do-i-watch-elements-of-an-angulardart-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!