简体   繁体   English

深度嵌套对象的Angular 2变化检测

[英]Angular 2 Change Detection for deep neested object

I am using change detection in Angular 2 to watch for changed in a deep level array. 我在Angular 2中使用更改检测来监视深度数组中的更改。 Please see this plunker... 请看这个矮人...

http://plnkr.co/edit/zcl9pT?p=preview http://plnkr.co/edit/zcl9pT?p=preview

Is there anyway to do what I am doing without using changeDetectionStrategy? 无论如何,不​​使用changeDetectionStrategy做我正在做的事情吗? Its seems like overkill having to inject it in the parent component. 似乎必须将其注入父组件中,这似乎有些过分。 I really only need it for the directive that loops through the list of selected filters. 我真的只需要用于遍历所选过滤器列表的指令。

 @Component({
  selector: 'hello-world',
  templateUrl: 'src/hello_world.html',
  providers: [MyService],
  pipes: [keyValueFilterPipe, ValuesPipe, AsyncPipe],
  directives: [MyDirective, MyDirective2],
  changeDetection: ChangeDetectionStrategy.CheckAlways
})
export class HelloWorld {

    constructor(){}

  }
}

Update: this bug is fixed in beta17, as @yurzui pointed out. 更新:正如@yurzui指出的,此错误已在beta17中修复。 http://plnkr.co/edit/SwbjKn2M9NBBOYD29IML?p=preview http://plnkr.co/edit/SwbjKn2M9NBBOYD29IML?p=preview

Related PR: https://github.com/angular/angular/commit/42231f5 相关PR: https : //github.com/angular/angular/commit/42231f5

It allows all legal programs in the dev mode. 它允许在dev模式下的所有法律程序。

The checkNoChanges function compares only primitives types for equality, and deeply compares iterables. checkNoChanges函数仅比较基本类型是否相等,并深度比较可迭代对象。 Other objects cannot cause checkNoChanges to throw. 其他对象不能引起checkNoChanges抛出。 This means that the dev mode would never fail given a legal program, but may allow some illegal programs. 这意味着开发模式在给定合法程序的情况下不会失败,但可能会允许某些非法程序。


Original Answer 原始答案

The problem lies in your pipe's implementation. 问题出在管道的实现上。 Every time keyValueFilterPipe returns a new JavaScript object, that is not the same as the last one returned by the same filter. 每次keyValueFilterPipe返回一个新的JavaScript对象时,该对象与同一过滤器返回的最后一个对象都不相同。 Angular think this is a bug in your code and ceases to work. Angular认为这是您代码中的错误,并且不再起作用。

You can see the error reported in console like ... has changed after it was checked . 您可以看到控制台中报告的错误,例如... has changed after it was checked Not only your directive stops to work, normal text-binding also breaks. 不仅您的指令停止工作,普通的文本绑定也中断了。

Angular2 in dev mode will try to figure out what is changed during its "dirty check". 在开发模式下的Angular2将尝试找出在“脏检查”过程中所做的更改。 So if a developer mistakenly puts a cyclic call in template, Angular will report that. 因此,如果开发人员错误地将循环调用放入模板中,Angular将报告该情况。

This is a feature in Angular2. 这是Angular2中的功能。 But it harms you. 但这伤害了你。

The solution is easy. 解决方案很简单。 Either change the detection strategy, like you do. 像您一样更改检测策略。 Or call enableProd to suppress the warning. 或调用enableProd禁止显示警告。 Or implement keyValueFilter that always returns a fixed object(by WeakMap eg) 或者实现总是返回固定对象的keyValueFilter (例如WeakMap


See also 也可以看看

http://www.bennadel.com/blog/3040-i-have-a-fundamental-misunderstanding-of-change-detection-in-angular-2-beta-8.htm http://www.bennadel.com/blog/3040-i-have-a-fundamental-misunderstanding-of-change-detection-in-angular-2-beta-8.htm

And Angular's original issue, especially this comment 还有Angular的原始问题,尤其是此评论

https://github.com/angular/angular/issues/5918#issuecomment-167422071 https://github.com/angular/angular/issues/5918#issuecomment-167422071

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM