Skip to content Skip to sidebar Skip to footer

How Can I Write An Angular NgFor Pipe To Filter Array Of Objects By Object Property?

I have 2 selects. One for Leagues and one for Divisions I want to create a Pipe that will filter Divisions depending on what League is selected. Giving the data below. If I select

Solution 1:

I have create a custom pipe and used it to filter division dropdown.

@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>

Please look into this Plnkr


Post a Comment for "How Can I Write An Angular NgFor Pipe To Filter Array Of Objects By Object Property?"