Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular filter ngfor

// In your component:
filterargs = {title: 'hello'};
items = [{title: 'hello world'}, {title: 'hello kitty'}, {title: 'foo bar'}];
In your template, you can pass string, number or object to your pipe to use to filter on:

// In your .html:
<li *ngFor="let item of items | myfilter:filterargs">

// In your pipe:
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'myfilter',
    pure: false
})
export class MyFilterPipe implements PipeTransform {
    transform(items: any[], filter: Object): any {
        if (!items || !filter) {
            return items;
        }
        // filter items array, items which match and return true will be
        // kept, false will be filtered out
        return items.filter(item => item.title.indexOf(filter.title) !== -1);
    }
}

// Remember to register your pipe in app.module.ts; 
// you no longer need to register the pipes in your @Component
import { MyFilterPipe } from './shared/pipes/my-filter.pipe';

@NgModule({
    imports: [
        ..
    ],
    declarations: [
        MyFilterPipe,
    ],
    providers: [
        ..
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript create element with class 
Javascript :: how to get seconds in timstamps js 
Javascript :: useref array of refs 
Javascript :: upload and read json file javascript 
Javascript :: return only specific attributes when making query mongoose 
Javascript :: how to use typeof in javascript 
Javascript :: return fetch javascript 
Javascript :: how to handle navigation between multiple stack react native 
Javascript :: ajax multipart/form-data 
Javascript :: javascript split by backslash 
Javascript :: how to find length of array in javascript without using length method 
Javascript :: javascript string contains character 
Javascript :: regex pattern for strong password 
Javascript :: Could not find the drag and drop manager in the context of ResourceEvents. Make sure to wrap the top-level component of your app with DragDropContext app.js 
Javascript :: Select All Elements With A Class getElementsByClassName 
Javascript :: async for loop 
Javascript :: nodejs aws s3 stream upload 
Javascript :: delete a property of html by js 
Javascript :: format a date moment 
Javascript :: jquery noconflict 
Javascript :: javascript copy some properties from one object to another 
Javascript :: javascript element distance from top 
Javascript :: convert string in hh:mm am/pm to date js 
Javascript :: js generate random numbers 
Javascript :: converting javascript object to json 
Javascript :: javascript global variable across files 
Javascript :: node array 
Javascript :: string reverse javascript 
Javascript :: javascript loop over dictionary 
Javascript :: javascript compare number to string 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =