【发布时间】:2017-05-15 00:35:03
【问题描述】:
今天我正在尝试了解自定义管道。我可以让一切正常工作,直到我到达为 PipeTransform 创建参数的部分。无论我在网上阅读什么内容,我似乎都无法理解我离开的地方。
我希望有一个过滤器可以重复用于每种卡类型、Visa、MC、AMEX 等,并从管道中的变量调用它。
*ngFor="let transaction of ('./app/api/transactions.json' | fetch | cardType:['Visa'])"
我的问题在这里,在 cardtype.pipe.ts 文件中:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'cardType' })
export class CardTypePipe implements PipeTransform {
transform(value: string, args: string[]): any {
// I can't figure out the args here...
}
}
这是显示 2 次 Visa 交易和 1 次 MC 的示例数据。我的目标是只在表格中显示 VISA 卡类型。我正在为数据调用 JSON 文件:
[
{
"cardType": "Visa",
"amount": 153.42
},
{
"cardType": "MasterCard",
"amount": 296.11
},
{
"cardType": "Visa",
"amount": 74.57
}
]
我的桌子:
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Card Type</th>
<th class="text-xs-right">Amount</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let transaction of ('./app/api/transactions.json' | fetch | cardType:['Visa'])">
<td>{{ transaction.cardType }}</td>
<td class="text-xs-right">{{ transaction.amount | currency:'USD':true }}</td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: json angular typescript angularjs-filter