I am working on learning Angular and Typescript. Now, I want to do this:
- get user list from web api
public list$:Observable<{id:string,name:string,like:number}[]> = new Observable()
ngOnInit(): void {
this.getData()
}
getData(){
this.list$ = this.service.getUserList()
}
- render user list
<ul>
<li *ngFor="let item of list$ | async">
<input (ngModelChange) = "changeName($event,item.id)">
....
</li>
</ul>
- change the user name by id
async changeName(newName:string,id:string){
const res = await this.service.changeName(newName,id).toPromise()
if(res.success){
// How to update this.list$ , without send a new request
// just like this:
// this.userList = this.userList.map(i=>i.id === id ? { ...i, name:newName } : i)
}
}
Is this something that can be done?
If yes, is it something that SHOULD be done, or is there a better way to accomplish what I am trying to do?
thinks!!!!