Angular2 use base pipe in custom handset

I would like to add some additional features to the basic angular2 channels.

T. some additional formatting performed on the currency pipe. For this, I would like to use the existing channel in the component code of my custom channel.

Can this be done?

@Pipe({name: 'formatCurrency'}) export class FormatCurrency implements PipeTransform { transform(value:number, args:string[]) : any { var formatted = value/100; //I would like to use the basic currecy pipe here. ///100 | currency:'EUR':true:'.2' return 'Do some extra things here ' + formatted; } } 
+7
angular angular-pipe
source share
2 answers

You can extend CurrencyPipe something like this:

 export class FormatCurrency extends CurrencyPipe implements PipeTransform { transform(value: any, args: any[]): string { let formatedByCurrencyPipe = super.transform(value, args); let formatedByMe; // do your thing... return formatedByMe; } } 

If you look at a source that is similar to how angular pipes work ...


(Posted by the author of the question)

Remember to import the CurrencyPipe class

 import {CurrencyPipe} from 'angular2/common'; 
+14
source share

Alternatively, you can enter CurrencyPipe:

 bootstrap(AppComponent, [CurrencyPipe]); 

Trumpet:

 @Pipe({ name: 'mypipe' }) export class MyPipe { constructor(private cp: CurrencyPipe) { } transform(value: any, args: any[]) { return this.cp.transform(value, args); } } 
+9
source share

All Articles