Angular4、试图理解变化检测

Angular 4, trying to understand change detection

我正在尝试了解变化检测和 ChangeDetectionStrategy.OnPush 是如何工作的,这里有一个我无法理解的奇怪例子:

plnkr

我使用以下模板创建父组件:

<app-matrix-reader *ngFor="let m of fooService.getMatrices()" [matrix]="m"></app-matrix-reader>

子组件 app-matrix-reader 有一个输入文本。

<input type="text" [(ngModel)]="matrix.a" /><br/>

如果我简单地多次点击输入(即使没有改变它的值),函数 getMatrices 被调用。

为什么调用函数getMatrices()?我还放置了 changeDetectionStrategy.OnPush,因此只有在输入更新时才应检查和更新组件...

谢谢

看看这个回答

Angular将运行改变检测周期当Ж

...

2) 绑定事件(如 (click) 或某些主机侦听器)从组件(即您的情况)触发

If I simply click through the inputs multiple times (even without change its value) the function getMatrices is called.

您正在触发 blur event on input,因此 angular 会将树标记为要检查的组件并执行更改检测