This is somewhat hard to describe because there are multiple (wrong) results and I just don't know where the problem lays. However, the expected result is made clear and if anything is missing, I'm available to update the question.
Situation
When displaying a profile-detail (Parent), navigating to another profile-detail should be possible by changing the parameter in the route /profile/id
. Therefore I subscribe to the param like mentioned in the accepted answer of this question.
// PARENT
constructor(private _route: ActivatedRoute) {
this.profileId = _route.snapshot.params['id'];
}
ngOnInit() {
this._route.params.subscribe(params => {
this.profileId = params['id'];
this.getResultByAsyncFunctionParent();
});
}
The profile-detail exists out of some HTML-code and a few Children. All data in the Parent updates but the code in the Child does not (should update). Most Childs use the same id
and are passed down using @Input
and ngOnChanges()
. I can confirm by debugging the flow of the application that the correct id
is used to get the expected data and all needed methods are called.
// CHILD
@Input() childprofileId: string;
ngOnChanges(changes: SimpleChanges) {
if (changes['childprofileId']) {
if (this.childprofileId != null) {
this.getResultByAsynFunctionChild();
}
}
}
The Parent uses the Child in HTML like
<app-projectview [childprofileId]="profileId"></app-projectview>
Question
Even if the model of the Child changes, the view is not updated. How do I force the Child to update (without having to navigate to a blank page and navigate back or something hacky like that)? I've tried ApplicationRef.tick()
, NgZone.run(callback)
and ChangeDetectorRef.detectChanges()
like mentioned in the accepted answer of this question but these didn't seem to work in my case.
Current results
There is not one but four possible (wrong) results displayed, which I think has something to do with my async functions. However, solving the problem should have nothing to do with these functions.
We start by navigating like the situation described, from one profile-detail to another.
- The Child doesn't update and still shows the data from the last profile.
- The Child does update and merges the data from the last and the current profile.
- The Child does update and merges the data from the last and an empty object.
- The Child does update and merges the data from the current and an empty object.
- IMG 1 - Startpoint: planning before navigation (correct)
- IMG 2 - Endpoint: planning after navigating (incorrect)
- IMG 3 - Expected result: planning after navigating (correct)
Async function example
// CHILD
getResultByAsynFunctionChild(): void {
this._planningService.getCollection().subscribe(
(toReturnPlanning: Planning[]) => {
this.planning = toReturnPlanning;
if (this.planning != null) {
... // Chart data logic, assign planningData in data
}
// Reassign the new data in the chart
this.barChartData[0].data = data;
}
});
}
EDIT
- Added the visual example of case 3
- Added the async function example