我正在尝试在Angular 2中创建一个自定义管道,它将对一组对象进行排序.我从
this post获得了一些帮助.然而,我似乎无法让这个工作.
我的烟斗看起来像这样:
@Pipe({ name: "orderByAsync",pure: false }) export class AsyncArrayOrderByPipe { private _promise : Promise<Array<Object>>; private _output: Array<Object>; transform(promise: Promise<Array<Object>>,args: any): Array<Object>{ var _property : string = ""; var _descending : boolean = false; this._property = args[0]["property"] || ""; this._descending = args[0]["descending"] || false; if(!this._promise) { this._promise = promise.then((result) => { result.sort((a: any,b: any) => { if (a[this._property] < b[this._property]) return (this._descending ? 1: -1); else if (a[this._property] > b[this._property]) return (this._descending ? -1: 1); else return 0; }); this._output = result; }); } return this._output; } }
管道的使用如下所示:
<div *ngFor="#c of countries | orderByAsync">{{c.name}}</div>
我错过了什么?