how to use pipe in angular 2 js?

user944513

I am trying to use pipe or filter in angular 2 js .I need to apply that filter on my list .I want to show only that item (which end with t character).In other words I need to display only that item which end with t

here is my code http://plnkr.co/edit/FKGrBDGyEIc3n2oaWUvf?p=preview

import { Pipe, PipeTransform } from 'angular2/core';

    export class EndWithT implements PipeTransform {
      transform(value: string, exponent: string): string {
        return value
      }
    }

In html

<ion-list style="border:2px solid grey;height:500px">
  <ion-item *ngFor="#item of Todo | EndWithT">
{{item.name}}
<button style='float:right' (click)="deleteTodo(item)">delete</button>
  </ion-item>
</ion-list>
Thierry Templier

You need to add the @Pipe decorator to your class:

@Pipe({
  name: 'endWithT'
})
export class EndWithT implements PipeTransform {
  transform(value: string, exponent: string): string {
    return value
  }
}

and add the class name within the pipes attribute of the component / page where you want to use it:

@Page({
  template: `
    <ion-list style="border:2px solid grey;height:500px">
      <ion-item *ngFor="#item of Todo | endWithT">
        {{item.name}}
        <button style='float:right' (click)="deleteTodo(item)">delete</button>
      </ion-item>
    </ion-list>
  `,
  pipes: [ EndWithT ]
})

You also need to update your transform methos this way:

transform(value: string, exponent: string): string {
  if (!value) {
    return value;
  }

  return value.filter((val) => {
    return /t$/.test(val.name);
  });
}

See this plunkr: http://plnkr.co/edit/3TQDQWq84YePtjDsrIgb?p=preview.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use a pipe in a component in Angular 2?

From Dev

Angular2 use basic pipe in custom pipe

From Dev

How to use a pipe in two different Angular Modules

From Dev

How to use the angular decimal pipe in typscript

From Dev

How to use pipe in levelup (node.js)?

From Dev

Angular2 rc.6 Use pipe in component

From Dev

Angular 2. Issue with *ngFor, when i use Pipe

From Dev

angular2 – use global service through a custom pipe

From Dev

Can I use '==' with pipe ( | ) in angular2 templates?

From Dev

Dynamic pipe in Angular 2

From Java

How to use moment.js library in angular 2 typescript app?

From Dev

How use js library with jquery on angular2 (typescript)?

From Dev

how to use underscore.js library in angular 2

From Dev

how to use SheetJS (js-xlsx) in angular 2

From Dev

How to use WOW.js in Angular 2 Webpack?

From Dev

How to use cat in a pipe

From Dev

How to create and call a pipe from the component in Angular 2?

From Java

How do I call an Angular 2 pipe with multiple arguments?

From Dev

How to pass array as arg in Pipe in Angular2

From Dev

How does angular2 detect the changes in a stateful pipe?

From Dev

How to sort a list of objects using an Angular 2 Pipe

From Dev

How to create pipe to filter list in Angular2

From Dev

Angular2/Typescript: How to get an array of objects through Pipe?

From Dev

How to set align right/left in currency pipe angular2

From Dev

How to use angular js with webstorm

From Java

Angular 2 Pipe under condition

From Dev

angular 2 date pipe weeknumber

From Dev

Angular 2 "time ago" pipe

From Dev

Angular 2 Input Custom Pipe