Angular 4排序表列

我在此示例的基础上排序表列:
https://plnkr.co/edit/nll91Dbc ... eview
这是一个代码管:


import {Pipe, PipeTransform} from 'angular2/core';

@Pipe/{ name: 'orderBy' }/
export class OrderrByPipe implements PipeTransform {

transform/records: Array<any>, args?: any/: any {

return records.sort/function/a, b/{
if/a[args.property] &lt; b[args.property]/{
return -1 * args.direction;
}
else if/ a[args.property] &gt; b[args.property]/{
return 1 * args.direction;
}
else{
return 0;
}
}/;
};
}


这个代码 html:


<tr *ngfor="let particular of particulars | orderBy: {property: column, direction: direction} | slice:1; let i = index">


在组件中导入:


import { OrderrByPipe } from '../pipes/orderby.pipe';


我想转移管道 Angular 4, 这怎么办吗?

这是控制台中的错误:


error_handler.js:60 Error: Uncaught /in promise/: Error: Error in ./ParticularsListComponent class ParticularsListComponent - inline template:42:14 caused by: Cannot read property 'sort' of undefined
Error: Error in ./ParticularsListComponent class ParticularsListComponent - inline template:42:14 caused by: Cannot read property 'sort' of undefined


</tr></any>
已邀请:

冰洋

赞同来自:

你的问题来自变量
particulars

, 哪个未定义。

二哥

赞同来自:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe/{
name: 'orderby'
}/
export class OrderbyPipe implements PipeTransform {


//private records : Array<any> = [];

transform/records :Array<object>, args?: any/: any {
if/records != null/{
return records.sort/function/a, b/{
if /a[args.property] === '' || a[args.property] === null || typeof a[args.property] === 'undefined'/ {
return 1 * args.direction;
}
if /b[args.property] === '' || b[args.property] === null || typeof b[args.property] === 'undefined'/ {
return -1 * args.direction;
}
if/a[args.property] &lt; b[args.property]/{
return -1 * args.direction;
}
else if/ a[args.property] &gt; b[args.property]/{
return 1 * args.direction;
}
else{
return 0;
}
}/;
}
};

}


</object></any>

要回复问题请先登录注册