angular 6 filter the async pipe results

slevin picture slevin · Nov 6, 2018 · Viewed 7.1k times · Source

I use angular 6 and I would like to filter the results of an async pipe, before rendering them in the UI.

Here is my code right now

this.results = this.form.get('name').valueChanges.pipe(           
  filter(formdata => formdata.name.length > 0), 
  switchMap( formdata => this.service.getNames(formdata.name)) 
);

and the template

  <div *ngIf='results | async ; let items'>
       <div *ngFor='let item of items'>{{item.id}} {{item.name}} </div>               
  </div> 

From the pipe I get some ids and names. I already have an array of ids. I would like to filter the ids of the pipe and not render the ones that are already in the array.

So, here is what I try to do.

array = [{id:1,name:'one'},{id:2,name:'two'}];//I already have this

new version of filter in pipe

this.results = this.form.get('name').valueChanges.pipe(           
  filter(formdata => formdata.name.length > 0), 
  switchMap( formdata => this.service.getNames(formdata.name)) ,
  filter(checkIfResultIdInArray())//pseudocode
);

Assume that checkIfResultIdInArray is I function I created. Filters and returns all the ids that are not in the array. So the ids/names that end up in the template are not the {id:1,name:'one'},{id:2,name:'two'}.

Or maybe I can filter in the template somehow?

Answer

Yoeri picture Yoeri · Nov 7, 2018

@Davy's answer is what I would do myself. However another option is to use a pipe. This is the way to go if you would like to reuse this functionality.

@Pipe({name:'filterOnId'})
export class FilterOnIdPipe implements PipeTransform {
    transform(list : MyObject[], acceptedIds : number[]){
        return list.filter(item => acceptedIds.indexOf(item.id) > -1);
    }
}

and in the template

<div *ngFor='let item of results | async | filterOnId : acceptedIds'>
    {{item.id}} {{item.name}} 
</div>

Note the following:

You use your custom pipe the same way you use built-in pipes. You must include your pipe in the declarations array of the AppModule If you choose to inject your pipe into a class, you must provide it in the providers array of your NgModule.