【发布时间】:2016-03-10 22:15:43
【问题描述】:
我正在尝试在 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>
就像从未通知视图承诺已解决并且数据已返回。
我错过了什么?
【问题讨论】:
-
能否请您创建一个快速 bin 以便可以播放 sn-p。
标签: angular