加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 使用管道名称/元数据在运行时调用管道

发布时间:2020-12-17 07:53:59 所属栏目:安全 来源:网络整理
导读:我正在尝试构建一个动态表,我可以在运行时决定使用哪个管道(如果有). 我正在尝试实现类似于(简化)的东西: export class CellModel { public content: any; public pipe: string} 表 tbody tr *ngFor="let row of data" template ngFor let-cell [ngForOf]=r
我正在尝试构建一个动态表,我可以在运行时决定使用哪个管道(如果有).

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

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是某种方式还是其他解决方案?

您无法动态应用管道.你可以做的是建立一个“元”管道,决定要做什么转换.
@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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读