我有两个选择。
一个用于Leagues
,另一个用于Divisions
我想创建一个Pipe
来过滤Divisions
,具体取决于所选的League
。
提供以下数据。如果我选择Random Beer League
仅TwoFour
,则SixPack
应显示为Divisions
选择的选项。
leagues = [
{id: 1, leagueName: 'Recreation League' },
{id: 2, leagueName: 'Random Beer League' }
];
divisions = [
{id: 1, divisionName: 'SoGreen', leagueId: 1, leagueName: 'Recreation League' },
{id: 2, divisionName: 'Yellow', leagueId: 1, leagueName: 'Recreation League' },
{id: 3, divisionName: 'TwoFour', leagueId: 2, leagueName: 'Random Beer League' },
{id: 4, divisionName: 'SixPack', leagueId: 2, leagueName: 'Random Beer League' }
];
*注意 - plunker中的数据是硬编码的,但在我的应用程序中设置了Observable。
答案 0 :(得分:6)
我创建了一个自定义管道并用它来过滤部门下拉列表。
@Pipe({
name: 'myfilter',
pure: false
})
export class MyFilterPipe implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => item.leagueName.indexOf(args.leagueName) > -1);
}
}
<option *ngFor="let division of divisions | myfilter:leagueSelected" [ngValue]="division">{{division.divisionName}}</option>
请查看此Plnkr