在运行时使用管道名称/ metadata调用管道 [英] invoke pipe during run time using pipe name /metadata

查看:61
本文介绍了在运行时使用管道名称/ metadata调用管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个动态表,在其中我要在运行时中决定要使用哪个管道(如果有)。

I'm trying to build a dynamic table where i wand to decide in run time which pipe to use (If Any).

我正在尝试实现类似于(简化)的内容:

I'm trying to achieve something similar to (Simplified):

export class CellModel {
     public content: any;
     public pipe: string
}

<tbody>
     <tr *ngFor="let row of data">
         <template ngFor let-cell [ngForOf]=row>
           <td *ngIf="cell.pipe">{{cell.content | cell.pipe}}</td>
           <td *ngIf="!cell.pipe">{{cell.content}}</td>
     </tr>
</tbody>

我知道此示例给出了一个错误。我可以通过某种方式或其他解决方案使用Reflect吗?

I understand that this example gives an error. Can i use Reflect is some manner or some other solution?

推荐答案

您不能动态应用管道。您可以做的是建立一个决定要执行哪些转换的元管道。

You can't apply pipes dynamically. What you can do is to build a "meta" pipe that decides what transformation(s) to do.

@Pipe({
  name: 'meta'
})
class MetaPipe implements PipeTransform {
  transform(val, pipes:any[]) {
    var result = val;
    for(var pipe of pipes) {
      result = pipe.transform(result);
    }
    return result;
  }
}

然后像

<td *ngIf="cell.pipe">{{cell.content | meta:[cell.pipe]}}</td>

这篇关于在运行时使用管道名称/ metadata调用管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆