5

我有2个选择。

一为Leagues一为Divisions

我想创建一个PipeDivisions根据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 显示设置

*注意 - plunker 中的数据是硬编码的,但在我的应用程序设置中它的 Observable。

4

1 回答 1

9

我创建了一个自定义管道并用它来过滤除法下拉列表。

@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

于 2017-01-12T05:27:38.090 回答