Tom*_*omP 8 html angular-material2 angular angular-material-table
我正在尝试制作可重复使用的材料表,并且我想使用TemplateRefwithngTemplateOutlet来生成列。在这个例子中,我创建了cards使用我的material-table组件的组件。在cards.component.html我有我的表格列之一的模板。运行该项目将导致错误ERROR TypeError: Cannot read property 'template' of undefined(请参阅 上的控制台stackblitz)。是否可以将 columnt 模板传递给我MaterialTableComponent并使用它来定义列?
<table mat-table [dataSource]="data" class="mat-elevation-z4">
<ng-container
*ngFor="let column of displayedColumnObjects"
[matColumnDef]="column.id"
>
<!-- if where is cellTemplate in table config json, use cellTemplate -->
<ng-container
*ngIf="column.cellTemplate"
[ngTemplateOutlet]="column.cellTemplate"
>
</ng-container>
<ng-container *ngIf="!column.cellTemplate">
<th mat-header-cell *matHeaderCellDef> {{column.title}} </th>
<td mat-cell *matCellDef="let element"> {{element[column.id]}} </td>
</ng-container>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
更新
我找到了使用ngTemplateOutletContext.
<table mat-table [dataSource]="data" class="mat-elevation-z4">
<ng-container
*ngFor="let column of displayedColumnObjects"
[matColumnDef]="column.id"
>
<ng-container
*ngIf="column.cellTemplate"
>
<th mat-header-cell *matHeaderCellDef> {{column.title}} </th>
<td mat-cell *matCellDef="let element">
<ng-template
[ngTemplateOutletContext]="{
element: element[column.id]
}"
[ngTemplateOutlet]="column.cellTemplate">
</ng-template>
</td>
</ng-container>
<ng-container *ngIf="!column.cellTemplate">
<th mat-header-cell *matHeaderCellDef> {{column.title}} </th>
<td mat-cell *matCellDef="let element"> {{element[column.id]}} </td>
</ng-container>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
看我的例子
在要重用的组件中添加以下内容
在“.HTML”文件中:
<ng-container [ngTemplateOutlet] = "yourNameReference"
[ngTemplateOutletContext] = "{$ implicit: {name: 'Arthur', lastName: 'Lemos'}">
</ng-container>
Run Code Online (Sandbox Code Playgroud)
在可重用组件的“.ts”文件中,在您的类中添加以下代码
@ContentChild ('yourNameReference') yourNameReference: TemplateRef <any>;
Run Code Online (Sandbox Code Playgroud)
父亲组件(收到重复使用的组件)
将以下代码添加到“.html”文件中
<ng-template #yourNameReference let-myDataa>
<p> {{myDataa.name}} {{myDataa.lastName}} </p>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
这样您就可以将数据从子组件发送到父组件,以便您可以为表创建动态列。
但是,只需按照上述步骤操作,应用表中的特定列...例如:
<td> <ng-container [ngTemplateOutlet] = "yourNameReference"
[ngTemplateOutletContext] = "{$ implicit: {name: 'Arthur', lastName: 'Lemos'}">
</ng-container> </td>
Run Code Online (Sandbox Code Playgroud)
添加到孩子的.ts:
@ContentChild ('yourNameReference') yourNameReference: TemplateRef <any>;
Run Code Online (Sandbox Code Playgroud)
在父组件中:
<my-table>
...
<ng-template #yourNameReference let-myDataa>
<p> {{myDataa.name}} {{myDataa.lastName}} </p>
</ng-template>
<! - this content will be rendered inside the table ... ->
...
</my-table>
Run Code Online (Sandbox Code Playgroud)
您可以为每列创建一个参考名称,然后重复该过程
角度版本:9 文档:https ://angular.io/api/common/NgTemplateOutlet
| 归档时间: |
|
| 查看次数: |
2484 次 |
| 最近记录: |