2016-08-16 16 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