【发布时间】:2019-07-22 21:28:34
【问题描述】:
如何以角度过滤数组对象中的值。 (例如:我需要过滤staffAttendanceList的staff对象中的值,我不知道如何传递正确的参数来过滤staff值。)
打字稿
1)在.ts中:
attendanceStatus:any;
staffAttendanceList: StaffAttendance[] = [
status:false,
staff:{
staffId:"ERP1001",
firstName:'Rahul',
lastName:'Sharma'
}
];
HTML:我使用了带参数('staff')的过滤管道,但它不起作用。
2) 在 HTML 中:
<table>
<tbody>
<tr*ngFor="let staffAttendance of staffAttendanceList|filter:searchStaffAttendance:'staff.staffId';
index as i">
<td>{{i+1}}</td>
<td>{{staffAttendance.staff.staffId}}
</td>
<td>{{staffAttendance.staff.firstName}}
{{staffAttendance.staff.lastName}}</td>
</tr>
</tbody>
</table>
过滤器管道:我创建了一个自定义过滤器管道来搜索人员值。
3)在过滤管中:
import { PipeTransform, Pipe } from "@angular/core";
@Pipe({
name:'filter'
})
export class FilterPipe implements PipeTransform {
transform = (value:[],filterString:any,propName:any):any => {
let inputValue = [];
if(filterString === undefined || filterString === '' ||
value.length == 0){
return value;
}
for(const item of value){
if(item[propName] === filterString){
inputValue.push(item);
}
}
return inputValue;
}
}
【问题讨论】:
-
item[propName] 正在尝试访问您的对象中不存在的 item[staff.staffId]。