【发布时间】:2018-03-09 15:05:09
【问题描述】:
我想写通用的pipe。这是我为数据类型Category编写的pipe
order-by-category.ts
import { Pipe, PipeTransform } from '@angular/core';
import { Category } from '../../models/Category';
import { sortBy } from 'lodash';
@Pipe({
name: 'orderByCategory',
})
export class OrderByCategoryPipe implements PipeTransform {
transform(value: Category[]): Category[] {
return sortBy(value, (o: Category) => { return o.name; });
}
}
.html
<ion-item *ngFor="let c of categorySortedList | orderByCategory">
<ion-label>{{c.name }}</ion-label>
<ion-radio [value]="c.id" (ionSelect)="selectedCategory(c)"></ion-radio>
</ion-item>
现在我需要写同样的pipe。但data type 不同。
现在是这样的:
transform(value: BudgetGroup[]): BudgetGroup[] {
return sortBy(value, (o: BudgetGroup) => { return o.name; });
}
所以我想在这里使用通用解决方案而不是同一种 2 pipes 并且我也需要维护 Typed pipe。
【问题讨论】:
-
将
Category替换为{ name: string }不起作用? -
我还没有在这里尝试任何东西。希望您将您的建议作为答案提供更多详细信息? @Rob
-
好吧,在这里提出问题之前,您绝对应该自己尝试一些事情..
-
其实
specific pipe上面我已经写过了。之后,我不知道如何将其转换为genericone.@Rob -
我看不出你的建议和这里的
any(no type) 有什么区别。你的想法? @Rob
标签: angular typescript ionic3