I'm trying to get familliar with angular 2's ChangeDetectionStrategy.OnPush
performance boost (as explained here). But I have curios case here.
I have parent AppComponent
:
@Component({
selector: 'my-app',
template: `<h1>
<app-literals [title]="title" [dTitle]="dTitle"></app-literals>
<input [value]="title.name"/>
</h1>
`
})
export class AppComponent implements OnInit {
title = { name: 'original' };
dTitle = { name: "original" };
constructor(private changeDetectorRef : ChangeDetectorRef) {
}
ngOnInit(): void {
setTimeout(() => {
alert("About to change");
this.title.name = "changed";
this.dTitle = { name: "changed" };
}, 1000);
}
}
And child LiteralsComponent
component:
@Component({
selector: 'app-literals',
template: ` {{title.name}}
{{dTitle.name}}`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class LiteralsComponent implements OnInit {
@Input('title') title;
@Input('dTitle') dTitle;
constructor() { }
ngOnInit() {
}
}
I thought setting strategy to OnPush
made angular only reflect changes of references but in a sample I tried changing (mutate) a property of an object and angular still reflects it.
this.title.name = "changed";
shouldn't be detected (thus the UI shouldn't not reflect the change).
Here is the case on plunker
How comes? How to do it right?