2017-02-16 11 views
2

を変換するために、私は私が変換するためにアプリケーション全体で使用することができ、この外のパイプを作成することができますどのように次のように見えるのAPIを使用して通貨を変換する方法、Angular2パイプは、通貨

exchange(Input: string, Output: string, value: number): number { 
     let inputRate = this.currencyStorage.getCurrencyRate(cnput); 
     let outputputRate = this.currencyStorage.getCurrencyRate(Output); 
     return value/ inputRate * outputputRate; 
    } 

を作成しました通貨?

答えて

1
@Pipe({name: 'currConvert'}) 
export class CurrConvertPipe implements PipeTransform { 
    constructor(private currencyStorage:MyCurrencyStorage) {} 

    transform(value: number, Input: string, Output: string): number { 
    let inputRate = this.currencyStorage.getCurrencyRate(cnput); 
    let outputputRate = this.currencyStorage.getCurrencyRate(Output); 
    return value/ inputRate * outputputRate; 
    } 
} 

モジュールdeclarationsに登録して

{{123 | currConvert:456 /*input*/:789 /*output*/}} 
+0

{{123 | currConvert:EUR:USD}}
のようにそれを使用し、私はこのように、それは私はあなたが '\t
{{123 | currConvert:'EUR':'USD'}}
を意味'リテラル文字列は引用符を必要とする。推測 – Sajeetharan

+0

未定義と言いました –

+0

1つの質問は、ページを更新せずにパラメータを変更するとパイプが機能しますか? – Sajeetharan