Angular2在自定义管道中使用基本管道 [英] Angular2 use basic pipe in custom pipe

查看:48
本文介绍了Angular2在自定义管道中使用基本管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在基本的angular2管道中添加一些其他功能.

I'd like to add some additional functionality to the basic angular2 pipes.

即.在货币管道上完成了一些额外的格式化.为此,我想在自定义管道的组件代码中使用现有管道.

ie. some extra formatting done on the currency pipe. To do that I'd like to use the existing pipe in the component code of my custom pipe.

有什么办法可以做到吗?

Is there any way this can 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;
  }
}

推荐答案

您可以扩展CurrencyPipe,如下所示:

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 the source, that's similar to how angular pipes work...

(由问题作者添加)

别忘了导入CurrencyPipe类

Don't forget to import the CurrencyPipe Class

import {CurrencyPipe} from 'angular2/common'; 

这篇关于Angular2在自定义管道中使用基本管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆