2016-06-13 2 views
0

제품 목록이 있습니다. 각 제품은 iddescription입니다.productId가 다른 경우 이벤트를 덮어 쓰지 않으려면 어떻게해야합니까?

<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 }); 
} 

나는 하나 개의 제품의 설명을 편집 할 때 그것은 잘 작동합니다.

그러나 product1의 설명을 편집 한 다음 바로 product2의 설명을 편집하면 (시간 간격이 1 초 미만) product2의 설명 만 업데이트됩니다.

나는 product2 님의 이벤트가 product1 님의 이벤트를 덮어 써서 한 곳에서만 구독하고 있기 때문에 발생했습니다.

어떻게 해결할 수 있습니까? 감사합니다

답변

2

distinctUntilChanged()을 사용하려면 product마다 다른 Observable이 필요합니다.

this.products.forEach(p => this.editStreams.push(new EventEmitter()) 
는 는 는
<div *ngFor="let product of products let i=index"> 
    <input type="text" [ngModel]="product.description" (ngModelChange)="onEdit($event, i, product.id)"> 
</div> 
는 는
private onEdit(description: string, id: string) { 
    this.editStreams[i].emit({ description, id }); 
} 
+0

흠, 당신은 "각 제품에 대한 관찰 가능한 다른 '무엇을 의미합니까 ? –

+0

각'product'에 대해 하나의'editStream'이 있습니다. –

+0

'editStream'의 특정 번호 (제품 번호 기반)를 만드는 현명한 방법이 있습니까? editStream 배열을 사용합니까? –

관련 문제