在angular2上的自定义管道上延伸类似管道的货币或数字
我想在我的定制管道上拨打号码管道。我找到了这个答案 但我认为这个解决方案对我不起作用。我有个错误 “找不到管道“bigInteger”在angular2上的自定义管道上延伸类似管道的货币或数字,angular,typescript,pipe,Angular,Typescript,Pipe,我想在我的定制管道上拨打号码管道。我找到了这个答案 但我认为这个解决方案对我不起作用。我有个错误 “找不到管道“bigInteger” 更新 这应该是固定的,因为至少在Angular4中有一段时间 原创 DI和扩展其他类的类存在一个已知问题 Util这是固定的,您可以使用组合而不是继承: @Pipe({ name: "bigInteger" }) export class BigInteger implements PipeTransform { constructor(priva
更新 这应该是固定的,因为至少在Angular4中有一段时间 原创 DI和扩展其他类的类存在一个已知问题 Util这是固定的,您可以使用组合而不是继承:
@Pipe({
name: "bigInteger"
})
export class BigInteger implements PipeTransform {
constructor(private currencyPipe:CurrencyPipe) {}
transform(value: any): string {
return this.currencyPipe.transform(value);
}
}
也许已经过时了,但这对我来说很有效(Angular 5): 然后,您只需像普通的数字管道一样使用它,但可以根据需要进行自定义:
<span>{{someValue | bigInteger : '1.2-2'}}</span>
{{someValue | bigInteger:'1.2-2'}
除此之外,在视图模板中使用自定义管道时,我还遇到了一个错误,抱怨CurrencyPipe没有提供程序。为了避免这个错误,我在app.module.ts中将它添加到提供者列表中:Providers:[…,CurrencyPipe]
现在我基于它的自定义管道工作了!现在,我确信这可能不是最佳实践或其他什么,但我需要找到一些合理的方法来覆盖默认管道,或者有更好的方法?此外,CurrencyPipe可以在Angular的公共模块import{CurrencyPipe}from'@Angular/Common'中找到代码>
import { Pipe, PipeTransform } from '@angular/core';
import { DecimalPipe } from '@angular/common'
@Pipe({
name: 'bigInteger'
})
export class BigInteger extends DecimalPipe implements PipeTransform {
transform(value: any, args?: any): any {
let result;
result = super.transform(value, args);
//any transformations you need
return result;
}
}
<span>{{someValue | bigInteger : '1.2-2'}}</span>