angular - 角度 7 中的多个 NgTemplate
问题描述
我有 Angular 7 项目。我写了<ng-template>
,它正在发送我的rowData
. 当我ng-template
像下面这样写一个时,一切都运行良好。但是,我需要多个 ng-template。我尝试了类似下面的方法。但是,我无法执行。我怎样才能做到这一点?
这完美地工作
app.grid.component.html
<ng-template pTemplate="body" let-rowData>
<tr>
<td *ngFor="let col of Columns" class="ui-resizable-column">
<span>{{rowData[col.field]}}</span>
</td>
<td>
<ng-template *ngTemplateOutlet="templateRef; context : {rowData : rowData}"></ng-template>
</td>
</tr>
</ng-template>
app.grid.component.ts
export class GridComponent implements OnInit {
@ContentChild(TemplateRef) templateRef: TemplateRef<any>;
}
我的组件.html
<app-grid>
<ng-template let-rowData="rowData">
<button type="button" label="Update" (click)="update(rowData)"
</ng-template>
</app-grid>
但我想要像下面这样的多个
app.grid.component.html
<ng-template pTemplate="body" let-rowData>
<tr>
<td *ngFor="let col of Columns" class="ui-resizable-column">
<span>{{rowData[col.field]}}</span>
</td>
<td>
<ng-template *ngTemplateOutlet="templateRef; context : {rowData : rowData}"></ng-template>
</td>
<td>
<ng-template *ngTemplateOutlet="templateRef2; context : {rowData : rowData}"></ng-template>
</td>
</tr>
</ng-template>
app.grid.component.ts
export class GridComponent implements OnInit {
@ContentChild(TemplateRef) templateRef: TemplateRef<any>;
@ContentChild(TemplateRef) templateRef2: TemplateRef<any>;
}
我的组件.html
<app-grid>
<ng-template let-rowData="rowData" [ngForTemplate]="templateRef">
<button type="button" label="Delete" (click)="delete(rowData)"></button>
</ng-template>
<ng-template let-rowData="rowData" [ngForTemplate]="templateRef2">
<button type="button" label="Update" (click)="update(rowData)"></button>
</ng-template>
</app-grid>
解决方案
我找到了答案。你可以从这个 stackblitz 链接中看到。(这个堆栈闪电战并不完全相同。但逻辑完全一样。)
推荐阅读
- syntax - C++ 为什么 Placement New 需要 STL 标头?
- c++ - 是否有插入函数定义的快捷键?
- java - 带有数据流的 Apache Beam Go SDK
- javascript - 如何使用 fetch 从 api 获取数据
- laravel - Blade 将 Collection 作为 String 而不是 Array
- javascript - 如何正确地将 JavaScript 合并到 html 中?
- git - git config --list 再找一个user.naemail=xb1 如何删除user.naemail?
- swift - 带有请求参数和标头的分段上传 (Alamofire)
- php - 为什么我的下拉列表在提交后会重置?
- python - 有条件地遍历染色体并在一个数据帧中定位到染色体和另一个数据帧中的间隔