angular2 - pipe error

sooon

I have this PipeGalleryFilter.ts:

import { Pipe,Injectable,PipeTransform } from '@angular/core';
@Pipe({
    name: 'galleryfilter',
    pure: false
})
@Injectable()
export class PipeGalleryFilter implements PipeTransform {
    transform(items: any[], args: any[]): any {
        return items.filter(item => item.type.indexOf(args[0].type) !== -1);
    }
}///@@

then I add it to my Gallery:

html

<masonry-brick class="brick50" *ngFor="let brick of bricks | galleryfilter: gallery_args">
  <img src="{{brick.img}}" alt="">
</masonry-brick>

ts

gallery_args = {type: 'magazine'};

bricks = [
     {title: 'Brick 1',img:"assets/img/img003.jpg",type: "magazine"},
     {title: 'Brick 2',img:"assets/img/img004.jpg",type: "magazine"},
     {title: 'Brick 3',img:"assets/img/img005.jpg",type: "newspaper"},
     {title: 'Brick 4',img:"assets/img/img003.jpg",type: "newspaper"},
     {title: 'Brick 5',img:"assets/img/img004.jpg",type: "newspaper"},
     {title: 'Brick 6',img:"assets/img/img005.jpg",type: "newspaper"},
     {title: 'Brick 1',img:"assets/img/img003.jpg",type: "movies"},
     {title: 'Brick 2',img:"assets/img/img004.jpg",type: "commercials"}, 
     {title: 'Brick 3',img:"assets/img/img005.jpg",type: "commercials"},
     {title: 'Brick 4',img:"assets/img/img003.jpg",type: "commercials"},
     {title: 'Brick 5',img:"assets/img/img004.jpg",type: "lifestyle"},
     {title: 'Brick 6',img:"assets/img/img005.jpg",type: "lifestyle"}
   ];

But I keep getting:

EXCEPTION: Error in ./Gallery class Gallery - inline template:22:39 caused by: Cannot read property 'type' of undefined

I kind of suspect it has something to do with the line return items.filter(item => item.type.indexOf(args[0].type) !== -1);, but I am not sure how to get it right.

How can I resolve this?

developer033

Since gallery_args is an object (as you showed in your question), not an array, so gallery_args[0] of course will be undefined.

Modify your transform function to:

transform(items: any[], args: any): any { // Note that the type of args should be any or {}
  return items.filter(item => item.type.indexOf(args.type) !== -1);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

NgFor doesn't update data with Pipe in Angular2

From Java

The pipe ' ' could not be found angular2 custom pipe

From Java

HttpInterceptor pipe error in Angular 10 in catchError() function

From Dev

Assign the output of a pipe in angular2 to a variable

From Dev

Angular2 Http error

From Dev

Error value has changed after checked (Stateful Pipe in Angular2)

From Dev

Angular2 use basic pipe in custom pipe

From Dev

Angular2 - assign pipe from a variable

From Dev

angular2 pipe for multiple arguments

From Dev

Angular2 split string (pipe?)

From Dev

Angular2 pipe causing '...has changed after it was checked' dev error

From Dev

Angular2: pass in a property name to a pipe

From Dev

Angular2: No Pipe decorator found on Component

From Dev

Testing Angular2 / TypeScript pipe with Jasmine

From Dev

Error handling with Angular2 async pipe

From Dev

Angular2 error on subscribe

From Dev

Angular2 rc.6 Use pipe in component

From Dev

Angular2 - assign pipe from a variable

From Dev

Angular2: No Pipe decorator found on Component

From Dev

Angular2 no error response

From Dev

Angular2 pipe not working for input

From Dev

Angular2 filter pipe on input

From Dev

Angular2 async pipe with observable

From Dev

Angular2: Error while using a text filter pipe to filter an array

From Dev

Angular2: Resolve a string to a pipe

From Dev

Angular2 Pipe to convert currency

From Dev

Angular2 pipe regex url detection

From Dev

Getting 'Could Not Be Found' Error re: Custom Pipe in Angular 2 App

From Dev

Angular2 pipe dynamic value not taking