2016-08-16 37 views
4

自定义管道延长像货币或数字管道,我想呼吁我的自定义管道numberPipe我觉得这个答案上angular2

Angular2 use basic pipe in custom pipe

,但我这个解决方案没有为我工作。我有一个错误 “管道 'BIGINTEGER' 无法找到”

import { Pipe, PipeTransform } from "@angular/core" 
import { CurrencyPipe } from "@angular/common" 

@Pipe({ 
name: "bigInteger" 
}) 
export class BigInteger extends CurrencyPipe implements PipeTransform { 
    transform(value: any): string { 
    return value 
} 
} 

回答

3

更新

这应该是固定的,因为一段时间,至少在Angular4

DI有一个已知的问题,扩展了其他类的类别

https://github.com/angular/angular/issues/8694

的Util这是固定的,你可以使用组成,而不是继承:

@Pipe({ 
    name: "bigInteger" 
}) 
export class BigInteger implements PipeTransform { 

    constructor(private currencyPipe:CurrencyPipe) {} 

    transform(value: any): string { 
    return this.currencyPipe.transform(value); 
    } 
} 
+0

除了这个,我也参加了视图模板使用我的自定义管道时得到一个错误,抱怨,有没有CurrencyPipe的提供者。所以为了解决这个错误,在我的app.module.ts中,我将它添加到Providers列表中:'providers:[...,CurrencyPipe]'现在我的基于它的自定义管道起作用了!现在,我确信这可能不是最佳实践,但我需要看到一些合理的方法来覆盖默认管道,或者有更好的方法吗? – FireDragon

+0

此外,CurrencyPipe可以在'@ angular/common'中的Angular的公共模块'import {CurrencyPipe}中找到;'' – FireDragon