我正在实现一个指令来向mat—table的mat—cell添加操作:
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef></mat-header-cell>
<mat-cell
*matCellDef="let row"
tableActions
[editAction]="true"
[actionInputData]="actionInputData"
[row]="row"
>
</mat-cell>
</ng-container>
tableActions是指令.我的目标是将动作附加为mat—cell的子项.
我已经try 了很多方法,我已经搜索了很多,但还没有找到任何方法来做到这一点,所有的工作都如预期:
-
我使用_viewContainerRef创建和附加操作,但操作总是添加到mat—cell旁边,而不是作为一个子.
let componentRef: ComponentRef = this._viewContainerRef.createComponent(component);
- 我使用了Angular的渲染器(Renderer2).它将组件附加为mat—cell的子组件,但mat—icon、mat—tooltip和Angular的标准指令(如 * ngIf)不起作用.似乎所有添加到操作html的指令都不起作用.
这里,我用angular核心的prement—function创建了componentRef,并将其附加到target—Element(mat—cell).
let componentRef: ComponentRef<any> = createComponent(component, {
environmentInjector: this._appRef.injector,
});
this._renderer.appendChild(
this._elementRef.nativeElement as HTMLElement,
componentRef.location.nativeElement
);
编辑操作的html是:
<button
mat-icon-button
(click)="editRow(row)"
color="primary"
matTooltip="{{ 'edit' | translate }}"
>
<mat-icon color="primary">edit</mat-icon>
</button>
My final question is:.我如何附加这些动作组件,使它们都能正常工作,就像我将直接实现到mat—cell中一样?