I have an *ngFor
directive being used to display a component for each element in an array: -
<app-user-row *ngFor="let user of users"
[user]="user"
(update)="updateUsers()"></app-user-row>
If one of those components fires an update
event, then the host component will call a service, which hits a server and gets an up to date copy of the data as an array. This also gets called on ngOnInit
to get the inital data
private updateUsers() {
this.userManagementService.getUsers().subscribe((res) => {
this.users = res;
},
(error) => {
console.log(error);
});
}
However, this seems to prepend the new data to the top of the old. For example, if I have 2 rows, one for Andy, and one for Bob, and I delete the Bob row, I'll now see Andy, Andy, Bob. At this point, my users array will also only contain the single entry for Andy. I'm pretty new to Angular2, what am I missing?
Edit: This is the service method that does the delete:
public deleteUser(user:User){
let headers = new Headers({
'Accept': 'application/json'
});
let options = new RequestOptions({headers});
return this.http.delete(`${this.apiBaseUrl}Account/user/${user.id}`, options);
}
And the function that calls it (in the UserRowComponent
):
onDelete() {
this.userManagementService.deleteUser(this.user).subscribe((res) => {
this.update.emit(this.user);
},
(error) => {
});
}