Answer the question
In order to leave comments, you need to log in
Angular (8) why is the template not updating?
I have a component, it has a sensors property: Array
There is a line in the template
<div class="pr-sensors-item" *ngFor="let item of sensors">
getSensors() {
this.sensors = [{}, {}] // так работет и шаблон рендерится
this.sensorsService
.get(this.machineParentId)
.subscribe((response) => {
this.sensors = response.sensors // так не работает
// or
this.sensors.push(...response.sensors)
});
}
Answer the question
In order to leave comments, you need to log in
Do you happen to have a component in it changeDetection: ChangeDetectionStrategy.OnPush
?
Angular detects changes in inputs by reference when OnPush. When you create a new list with this.sensors = [{}, {}]
, you refer to the new list, and when you change the value through this.sensors.push
the object reference does not change.
So either use the spread operator
this.sensors = [...this.sesnsors, ...response.sensors]
this.changeDetector.markForCheck()
private changeDetector: ChangeDetectorRef
)
*ngFor="let item of sensors$ | async;"
sensors$ = this.sensorsService.get(this.machineParentId);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question