簡體   English   中英

Angular 2 - 來自Observable的排序列表

[英]Angular 2 - Sort list from Observable

sort來自Observable的項目列表進行sort並仍然能夠使用async pipe的最佳方法是什么? (我讀到制作自定義排序管道效率不高。)我想避免訂閱和保留數據的本地副本,因此只使用異步管道...

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

模板:

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

如果你調用.subscribe()獲得一個Subscription ,異步管道需要一個Observable

如果你改成它

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

你可以使用test$的異步管道

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM