I don't quite understand why i need to add markForCheck() to the code below to make the changes visible. Why is my @Input() not triggering change detection?
I am refactoring my project to OnPush. These 2 components both have OnPush enabled. As i understand it, when this is enabled, and an Input() is changed (like messages
) the change detection gets triggered.
In the code below i make a call via the graphqlService
. When i receive the call i do some parsing with the incoming data and then set it to informationMessages
property, which is binded to the child component cv-messages
through its messages
property.
The result is that the ngOnChanges
function only get called once, when the informationMessages
property gets initialized. But not when the final parsed data is set to it.
If I add markForCheck() it works fine.
Consider this parent component, with a template like this:
<cv-messages [messages]="informationMessages"></cv-messages>
And a typescript file with this piece of code:
informationMessages: InformationMessageType[] = [];
ngOnInit() {
this.graphqlService.loadInformationMessages().subscribe(data => {
const informationMessages: InformationMessageType[] = data;
.... // parsing stuff
this.informationMessages = informationMessages;
// add markForCheck() here
});
}
The messages component has an ngOnChanges function like this:
ngOnChanges(changes) {
console.log(this.constructor.name ' CHANGE:', changes);
}
Update:
You can find the solution in the comments of an answer below. Basically, change detection is NOT triggered when an @Input()
changes asynchronously. So in that case we need to add a markForCheck()
to force a change detection.