我将使用竖管过滤一些json并根据值显示名称
这是我的JSON
bankAccountUsage = [
{ value: 1, name: "x" },
{ value: 2, name: "y" },
{ value: 3, name: "z" }
];

这是PIPE:
import { Pipe, PipeTransform } from '@angular/core';
import { StaticValuesService } from '../../services/static-values.service';
import { } from '../../services/static-values.service';
@Pipe({
name: 'accountUsageType'
})
export class AccountUsageTypePipe implements PipeTransform {
private Accounusages;
transform(value: number, args?: any): any {
this.Accounusages.filter(val => {
if (val.value === value) {
console.log(val);
return val.name;
}
});
}
constructor(private _statcvalues: StaticValuesService) {
this.Accounusages = this._statcvalues.bankAccountUsage;
}
}

这是Console.log(val)
的结果Object {value: 1, name: "x"}
HTML:
<td>{{account.AccountType | accountUsageType}}</td>
&#13;
但结果是空的
感谢
答案 0 :(得分:1)
return
声明; value
是原始variable
,因此,管道必须返回一个variable
。您可以使用Array#find
方法,如下所示:return this.Accounusages.find(val => val.value === value);