我知道,如果我们在发展角度运行变化检测两次。在以下示例中,Angular运行更改四次检测。这是为什么发生?角度变化检测运行八倍而不是四个
class Category {
constructor(private _id) {
}
get id() {
console.log('id');
return this._id;
}
}
@Component({
selector: 'app-select',
template: `
<select class="form-control">
<option *ngFor="let option of options;" [value]="option.id">{{option.id}}</option>
</select>
`,
})
export class SelectComponent {
@Input() options;
}
@Component({
selector: 'my-app',
template: `
<app-select [options]="options"></app-select>
`,
})
export class App {
options = [new Category(1)]
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, SelectComponent ],
bootstrap: [ App ]
})
export class AppModule {}
如果您运行上面的代码,您将看到控制台日志运行八次而不是四次。
变化检测运行多次第一次启动时,角度运行'app.tick'两次 –
需要每个异步任务 – yurzui
所以它应该console.log四次,而不是八次。 – ng2user