It looks like this problem is popular, I tried many times to update my view after a new element pushed in my array, but nothing happened.
I will explain my functions and what I'm trying to do, and I will show you my tries.
My first component is car.component.ts that I used to show my cars list calling API using carsService.
carsList() {
this._carsListService.carsService(this.user)
.subscribe(
response => {
this.result = response;
if (this.result.code != 200) {
//error response message
}
else if (this.result.code == 200) {
this.data = [] = this.data;
this.result.cars.forEach(element => {
this.data.push(element);
});
}
},
error => console.log(error)
);
}
Second component is insert.component.ts that I can add a new car with details and the carsList should detect the changes which is what I'm looking for.
insertNew() {
this._insertService.insertNewService(this.user)
.toPromise()
.then(
response => {
this.result = response;
if (this.result.status != 200) {
//error message
} else {
// new element inserted and now it is in cars list API
}
},
error => console.log(error)
);
}
Now in my car.component.html
<div *ngFor="let element of data" id="{{element.car_id}}">
<mat-card>
<mat-card-content>
//some details about cars
</mat-card-content>
</mat-card>
</div>
Now everything is fine for first reading, but when insertNew() called and inserted any new element, nothing change in carsList view.
1 - I tried to run my function using ngZone
carsList() {
this._carsListService.carsService(this.user)
.subscribe(
response => {
this.zone.run(() => { // <==
this.result = response;
if (this.result.code != 200) {
//error response message
}
else if (this.result.code == 200) {
this.data = [] = this.data;
this.result.cars.forEach(element => {
this.data.push(element);
});
}
console.log("using ngzone"); //this console appeared just one time even when I insert anything new
});
},
error => console.log(error)
);
}
2 - I tried to go with DoCheck algorithm included in Angular, it looks like
replacing this line <div *ngFor="let element of data" id="{{element.car_id}}"> with this <div *ngFor="#element of data" id="{{element.car_id}}"> but Angular said (Unexpected token #).
#EDIT
My service
carsService(value: Object) {
return this._appService.LoadAPI(value, this.carsAPI);
}
LoadAPI
public loadScript(src) {
let script = document.createElement('script');
script.type = 'text/javascript';
document.getElementsByTagName('body')[0].appendChild(script);
script.src = src;
}
EDIT 2
I have tried to call carList() function inside insertNew()
constructor( public cars_list: CarComponent)
insertNew() {
this._insertService.insertNewService(this.user)
.toPromise()
.then(
response => {
this.result = response;
if (this.result.status != 200) {
//error message
} else {
this.cars_list.carsList();
// new element inserted and now it is in cars list API
}
},
error => console.log(error)
);
}
carsListService.carsServiceobservable / subject notified? That will need to happen for thesubscribefunction to be called, thereby updating yourdatavariablethis.data = [] = this.datato load more button, to append the list separately.