2016-12-19 15 views
7

sortObservable'dan gelen öğelerin bir listesi ve async pipe'u kullanmanın en iyi yolu nedir? (Özel bir sıralama boru yapım gerçekten verimli olmadığını okuyun.) Ben sadece zaman uyumsuz borusunu kullanarak abone olduğunu ve böylece verilerin yerel bir kopyasını tutmak ve önlemek istiyorum ...Köşeli 2 - Gözat listesinden sıralama listesi

//can I use map here and filter items right in the observable and get rid of subscribe? 

this.test$ = Observable.of(['one', 'two', 'three']) 
    .subscribe((data) => { 
     data.sort((a, b) => { 
      return a < b ? -1 : 1; 
     }); 
     this.data = data; 
    }); 

şablonu:

<div *ngFor='let item of data'> 
<!-- want to be able to use async pipe here --> 

cevap

11

.subscribe()'u arayarak Subscription'u alırsanız, async borusu Observable bekler.

Eğer

this.test$ = Observable.of(['one', 'two', 'three']) 
.map((data) => { 
    data.sort((a, b) => { 
     return a < b ? -1 : 1; 
    }); 
    return data; 
}); 

bunu değiştirirseniz ben sıralamak için gözlemlenebilir haritasında operatörünü kullanabilirsiniz onaylayan için test$

+0

Teşekkür Günter ile zaman uyumsuz boru kullanabilirsiniz takdir ediyorum. – Thibs