extend a pipe like currency or number on a custom pipe on angular2

限于喜欢 提交于 2019-12-23 07:13:45

问题


I would like to call the numberPipe on my custom pipe I find this answer

Angular2 use basic pipe in custom pipe

but I this solution don't work for me. I have an error "The pipe 'bigInteger' could not be found"

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
 }
}

回答1:


update

this should be fixed since a while at least in Angular4

original

There is a known issue with DI and classes that extend other classes

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

Util this is fixed you can use composition instead of inheritance:

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

  constructor(private currencyPipe:CurrencyPipe) {}

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



回答2:


Maybe obsolete, but this worked for me (Angular 5):

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;
  }

}

Then you just use it like regular number pipe, but can customize as you wish:

<span>{{someValue | bigInteger : '1.2-2'}}</span>


来源:https://stackoverflow.com/questions/38971085/extend-a-pipe-like-currency-or-number-on-a-custom-pipe-on-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!