I have been reading post and googling an answer for this question without any success
I looked into these two links and nothing
Angular doesn't update view when array was changed
Updating HTML view when Angular array changed
I have two components (siblings) one component add Items and the other lists all items added. The idea is very simple, whenever someone adds an item I want to update the list in order to get that new item.
export class ListItem implements OnInit {
list: SomeObject[] = [];
constructor(private myService: SomeService) {}
ngOnInit() {
this.getItems();
}
getItems(): void {
this.myService.getDomesticatedPokemons()
.subscribe((newItems) => {
this.list = newItems; // I can see that this line works as expected but the view doesn't get updated though
});
}
}
export class AddItem implements OnInit {
constructor(private myService: SomeService, private list: ListItem) {}
ngOnInit() {
}
saveItem(aForm: NgForm): void {
// ...
// ...
// ...
// ...
this.myService.addItem({}).subscribe((result) => {
if (result.ok === 1) {
this.list.getItems();
} else {
console.error('Error: ...');
}
});
}
}
UPDATE:
this is the view:
<!-- This is the list template -->
<div id="list">
<div *ngFor="let item of list" class="item">
...
...
...
</div>
</div>
for the add component there is a form and saveItem() is executed on submit
UPDATE 2: I created a stackblitz this example is working even though I couldn't (or at least I didn't know how) reproduce my service getting data from server. I commented out "more realistic" code the main component is list.