Angular2 pipe dynamic value not taking

Maantu Das

I have a json like this

[{
  "id": 9156,
  "slug": "chicken-seekh-wrap",
  "type": "dish",
  "title": "Chicken Seekh Wrap",
  "cuisine_type": [2140]
},
{
  "id": 9150,
  "slug": "green-salad",
  "type": "dish",
  "title": "Green Salad",
  "cuisine_type": [2141]
}]

I created a pipe like this to filter by cuisine type in angular2

@Pipe({
      name: 'filter',
      pure: false
 })
 export class FilterPipe implements PipeTransform {
     transform(items: any[], selectedId: any): any {
     if(selectedId === -1)
           return items;
    return items.filter(item => item.cuisine_type[0] === selectedId);

  }
 }

And in the ionic view I have used it like this

<ion-card *ngFor="let cat of selectedCats">
            <ion-card-header>
              {{cat}}
            </ion-card-header>
            <ion-card-content>
                <ion-list class="checkbox-list" *ngIf="dishes ?.length > 0">
                  <ion-item class="checkbox-item" *ngFor="let dish of dishes | filter:cat">

                    <ion-label>{{dish.title}}</ion-label>
                      <ion-checkbox [formControlName]="dish.id"></ion-checkbox>

                  </ion-item>
                </ion-list>
            </ion-card-content>
</ion-card>

When I am using pipe value as static like its working

 <ion-item class="checkbox-item" *ngFor="let dish of dishes | filter:2140">

but when I am using it dynamically its not showing any result

<ion-item class="checkbox-item" *ngFor="let dish of dishes | filter:cat">
Günter Zöchbauer

The problem is not caused by Angular, but by your code

item.cuisine_type[0] === selectedId

being overly specific about accepted values

item.cuisine_type[0] == selectedId 

is more forgiving by allowing type coercion before the comparison.

This means, that cat is probably a string '2140' and item.cuisine_type[0] a number. If you pass it as | filter:2140" it's passed as number and matches item.cuisine_type[0].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamic pipe in Angular 2

From Dev

IN Clause is not taking dynamic passed value in SQL 2008

From Dev

angular2 - pipe error

From Dev

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

From Dev

Angular2 Use sort pipe only when certain value or boolean is true

From Dev

Angular2 Getting very deep nested json value using pipe! *ngFor

From Dev

Angular - number pipe if there's a value

From Dev

Ionic2 + Angular2 - dynamic rate value with ion-icon star

From Dev

angular2 pipe for multiple arguments

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

Angular2 split string (pipe?)

From Dev

Error handling with Angular2 async pipe

From Dev

Angular2 - assign pipe from a variable

From Dev

Assign the output of a pipe in angular2 to a variable

From Dev

Angular2 - assign pipe from a variable

From Dev

Angular2: No Pipe decorator found on Component

From Dev

Angular2 async pipe with observable

From Dev

Angular2 pipe not working for input

From Dev

Angular2 filter pipe on input

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

Angular2 dynamic templating

From Java

The pipe ' ' could not be found angular2 custom pipe

From Dev

Angular2 use basic pipe in custom pipe

From Dev

Angular 2 Pipe - can't return value inside a promise

From Dev

Angular 2 pipe modifies original value causing other lists to change