I have a list of products. Each product has an id
and description
.
<div *ngFor="let product of products">
<input type="text" [ngModel]="product.description" (ngModelChange)="onEdit($event, product.id)">
</div>
editStream: EventEmitter<any> = new EventEmitter();
ngOnInit() {
this.editStream
.debounceTime(1000)
.distinctUntilChanged()
.mergeMap(x => Observable.of(x))
.subscribe(x => {
// based on product id, update its description in the database
});
}
private onEdit(description: string, id: string) {
this.editStream.emit({ description, id });
}
It works well when I edit one product's description.
However, if I edit product1
's description, then edit product2
's description immediately (time interval is less than 1s), only product2
's description will be updated.
I guess this is because product2
's event overwrites product1
's event, and I only subscribe at one place.
How can I solve this? Thanks