Javascript 多模式角管

Javascript 多模式角管,javascript,angular,typescript,formatting,Javascript,Angular,Typescript,Formatting,我有一个实现DecimalPipe transform()方法的简单自定义管道,如下所示: export class MyDecimalFormatPipe implements PipeTransform { constructor(public decimalPipe: DecimalPipe) {}; transform(value: any) { if (value || value === 0) { value = this.

我有一个实现DecimalPipe transform()方法的简单自定义管道,如下所示:

export class MyDecimalFormatPipe implements PipeTransform {

    constructor(public decimalPipe: DecimalPipe) {};

    transform(value: any) {
        if (value || value === 0) {
            value = this.decimalPipe.transform(value, '1.2-2');
        }
        return value;
    }
} 
setFormatPattern(inputPattern: string): string {
    let pattern: any;
    this.myPatternsArray.forEach(element => {
        if (element.format === inputPattern ) {
           pattern = element.pattern;
        }
    });
    return pattern;
}
这是plunker链接:
显然,这不是一个定制的管道,因为它实现了DecimalPipe,但我的最终目标是让这个管道支持除“1.2-2”之外的更多模式。我做了一些研究,每个人似乎都在使用控制结构,比如if/else或switch语句。我想这看起来像:

export class MyDecimalFormatPipe implements PipeTransform {

    constructor(public decimalPipe: DecimalPipe) {};

    transform(value: any) {
        if (somevalue) {
            let format = '1.2-2'
            value = this.decimalPipe.transform(value, format);
        }
        else if (othervalue) {
            let format = '1.4-4'
            value = this.decimalPipe.transform(value, format);
        }
        ...
        return value
    }
} 
这似乎是错误的和“笨拙的”,随着我们添加更多的格式模式,这种方法会变得相当大。我认为管道应该有许多预设的格式模式,这些模式将根据输入值进行设置。我们可以为此创建一种方法,如下所示:

export class MyDecimalFormatPipe implements PipeTransform {

    constructor(public decimalPipe: DecimalPipe) {};

    transform(value: any) {
        if (value || value === 0) {
            value = this.decimalPipe.transform(value, '1.2-2');
        }
        return value;
    }
} 
setFormatPattern(inputPattern: string): string {
    let pattern: any;
    this.myPatternsArray.forEach(element => {
        if (element.format === inputPattern ) {
           pattern = element.pattern;
        }
    });
    return pattern;
}

我正在为这个问题的设计和实现(代码)而挣扎。一定有更简单的方法…

您可以像这样将参数传递到管道:

{{ whatever | mypipe: 'option1' }}
export class MyDecimalFormatPipe implements PipeTransform {

    constructor(public decimalPipe: DecimalPipe) {};

    transform(value: any, format: string) {
        return this.decimalPipe.transform(value, format);
    }
} 

// You then use your pipe like ...:

{{ something | myDecimalPipe: '1.4-4'}}
这将作为参数传递到pipe.ts中:

transform(value: any, args?: string) {
  switch (args) {
    case 'option1':
      return handleOption1(value);
    case 'option2':
      return handleOption2(value);
    ...
  }
}

您可以使用额外的参数,通过该参数将格式传递到管道中,可能您需要以下内容:

{{ whatever | mypipe: 'option1' }}
export class MyDecimalFormatPipe implements PipeTransform {

    constructor(public decimalPipe: DecimalPipe) {};

    transform(value: any, format: string) {
        return this.decimalPipe.transform(value, format);
    }
} 

// You then use your pipe like ...:

{{ something | myDecimalPipe: '1.4-4'}}

在这种情况下,我只会使用DecimalPipe格式,比如:{{无论如何| myDecimalPipe:'1.4-4'}}等等。我正在寻找一个解决方案,其中管道本身存储了许多格式模式。当使用管道时,您希望如何定义格式?使用类似于
private myPatterns=[{patternA:'1.4-4'},{patternB:'1.2-2},{patternC:'1.9-9}]
的东西怎么样?当我使用您的管道时,比如“{{myvar | myDecimalPipe}”,应该使用哪种模式?@MadCatm2-声明另一个管道只是为了将字符串“1.4-4”映射为“patternA”有什么好处?