我正在尝试在Angular 2中创建一个自定义管道,它将对对象数组进行排序。我从this post获得了一些帮助。但是,我似乎无法得到这个工作。在Angular 2中实现异步排序管道
我管看起来是这样的:
@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>
它的观点一样,永远不会通知该承诺已经解决,数据已经回。
我错过了什么?
能否请您创建一个快速所以这个片段可以被播放。 –