Introduction
Filter is an important part in AngularJS as well as Angular 2 or Angular 4. It is basically used to filter an item from a group of items, which are there in an array or an object array. It selects a subset of the items from an array and returns it as a new array and this item is displayed on UI.
@angular/core of Angular 4 or 2 module library provides two moduels - Piple and PipeTransform for the filtering feature.
- import { Pipe, PipeTransform } from '@angular/core';
Filter Code
Now, I will explain step by step by creating one filter.
Create one file MyFilterPiple.ts inside Filter folder and write the code, as shown below.
- import { Pipe, PipeTransform } from '@angular/core';
- import { Birds } from '../models/birds';
- @Pipe({
- name: 'myfilter',
- pure: false
- })
- export class MyFilterPipe implements PipeTransform {
- transform(items: any[], filter: Birds): any {
- if (!items || !filter) {
- return items;
- }
- return items.filter(item => item.name.indexOf(filter.name) !== -1);
- }
- }
Here, Filter name is myfilter and logic of filtering is written inside MyFilterPiple class.
{ Birds } is a model class, which contains the properties given below.
- export class Birds {
- id: number;
- name: string;
- }
- import { NgModule } from '@angular/core';
- import { BrowserModule } from '@angular/platform-browser';
- import { FormsModule } from '@angular/forms';
- import { HttpModule, JsonpModule } from '@angular/http';
- import { AppComponent } from './app.component';
- import {MyFilterPipe} from './filter/MyFilterPipe';
- @NgModule({
- imports: [
- BrowserModule,
- FormsModule,
- HttpModule,
- JsonpModule
- ],
- declarations: [
- AppComponent,
- MyFilterPipe
- ],
- bootstrap: [ AppComponent ]
- })
- export class AppModule {
- }
- import { Component } from '@angular/core';
- import {Birds} from '../models/birds';
- @Component({
- template: `
- <h2>Birds</h2>
- <p>List of Birds</p>
- <div>
- <ul>
- <li *ngFor="let bird of birds | myfilter:filterargs">
- {{bird.id}} {{bird.name}}
- </li>
- </ul>
- </div>
- `
- })
- export class BirdListComponent {
- filterargs = {name: 'Eagle'};
- birds: Birds[] = [
- {id: 11, name: 'Crow'},
- {id: 12, name: 'Peacock'},
- {id: 13, name: 'Sparrow'},
- {id: 14, name: 'Cuckoo'},
- {id: 15, name: 'Eagle'},
- {id: 16, name: 'Swan'},
- {id: 17, name: 'Duck'},
- {id: 18, name: 'Hen'},
- {id: 19, name: 'Parrot'},
- {id: 20, name: 'Woodpecker'}
- ]
- }
Inside *ngFor, you need to put the name of the filter and the filter arguments i.e. the value, which you want to filter. Here, filtering the value is hardcoded. It should be dynamic and come via textbox. For basic understanding, I hardcoded it, as shown below.
- filterargs = {name: 'Eagle'};
- <li *ngFor="let bird of birds | myfilter:filterargs">
Download and run the complete attached code. It is node.js, which must be installed into your PC.
Execute the project, as shown below.
Go to root folder "routingpractice" ~~ Open the command prompt ~~ type "npm start" ~~ it will automatically load on the Browser.
Execute the project, as shown below.
Go to root folder "routingpractice" ~~ Open the command prompt ~~ type "npm start" ~~ it will automatically load on the Browser.
Conclusion
Filter in Angular is a very powerful concept. It filters the value from an array or an object array and displays on UI. Angular 4 filter is very good and far better than AngularJS filter when it comes to performance.
tarvinder singhPosted Sep 12, 2019, 5:59 AM
Angular 2 don't provide filters. Here we are using custom pipes which filter your array. Angular Js filter performance is very poor so angular developer have removed it and provide concept of pipes or custom pipes which can have any logic as per you requirement.
Praveen RaiPosted Feb 9, 2018, 8:45 AM
As per best practice in angular filter logic shouldn't be the part of pipe it should be part of component only , please correct me if i am wrong
sailor babikPosted Jun 12, 2017, 2:22 AM
Is this (Angular2 == Angular4) result returns true? Thanks for helpfull article.
Thiruppathi RPosted May 18, 2017, 12:29 AM
Nice article.thanks for sharing...
Manav PandyaPosted May 18, 2017, 12:08 AM
Nice share sir ......