【问题标题】:Is there a way to dynamically evaluate/use an angular (angular 2+) pipe?有没有办法动态评估/使用角度(角度 2+)管道?
【发布时间】:2017-07-03 14:17:38
【问题描述】:

假设我有一个 pipeVar,它可以是任何管道,例如:数字、大写字母、customPipe 等

有没有一种简单的方法来调用类似的东西

{{ myVal | pipeVar }}

或一些特殊的语法,如

{{ myVal | #pipeVar }}

?

到目前为止我发现的最接近的是Dynamic pipe in Angular 2

【问题讨论】:

标签: angular


【解决方案1】:

我有更新 plunkr plunkr link

dynamic-pipe.ts 这样的变化

const dynamicPipe = "";
//i have give one simple logic for example if your dynamic pipe is like
this.dynamicPipe = ['number','uppercase','customPipe']; //pipe,pipe1 ... pipeN
//now create a one variable like 'number' | 'uppercase' | 'customPipe' 
for (let i=0;i<this.dynamicPipe.length;i++){
     dynamicPipe = dynamicPipe + " | "+this.dynamicPipe[i];
}
@Component({
    selector: 'dynamic-comp',
    template: '{{ data ' + dynamicPipe + '}}'
})

【讨论】:

  • 我明白了。感谢您指出我们可以让编译器处理多管道支持。我还没有针对 AOT 测试过该解决方案,你知道它是否适用于 AOT?
  • 什么是 AOT?我不知道 AOT。
  • 提前编译。 yurzui 提到它不适用于 AOT。不过我还没试过……
猜你喜欢
  • 2016-07-26
  • 1970-01-01
  • 2021-01-18
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多