我正在尝试构建一个动态表,我可以在运行时决定使用哪个管道(如果有).
我正在尝试实现类似于(简化)的东西:@H_502_2@
export class CellModel { public content: any; public pipe: string }
表@H_502_2@
<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>
您无法动态应用管道.你可以做的是建立一个“元”管道,决定要做什么转换.
@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; } }
然后像使用它一样@H_502_2@
<td *ngIf="cell.pipe">{{cell.content | Meta:[cell.pipe]}}</td>