I know that if we are on development Angular runs change detection twice. In the following example, Angular runs change detection four times. Why is this happening?
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 {}
If you run the code above you will see that the console log runs eight times instead four.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…