【问题标题】:Angular 2 - Sort list from ObservableAngular 2 - Observable 中的排序列表
【发布时间】:2017-05-04 15:10:28
【问题描述】:

sort 来自Observable 的项目列表的最佳方法是什么,并且仍然可以使用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 -->

【问题讨论】:

    标签: angular rxjs5


    【解决方案1】:

    如果你调用.subscribe(),你会得到一个Subscription,异步管道需要一个Observable

    如果你改成

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

    你可以使用带有test$的异步管道

    【讨论】:

    • 感谢 Günter 确认我可以使用 Observable 的地图运算符进行排序,感谢。
    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 2017-02-04
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2018-07-14
    • 2021-03-06
    • 1970-01-01
    相关资源
    最近更新 更多