我正在实现一个指令来向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 了很多方法,我已经搜索了很多,但还没有找到任何方法来做到这一点,所有的工作都如预期:

  1. 我使用_viewContainerRef创建和附加操作,但操作总是添加到mat—cell旁边,而不是作为一个子.

    let componentRef: ComponentRef = this._viewContainerRef.createComponent(component);

enter image description here

  1. 我使用了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
  );

enter image description here

编辑操作的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中一样?

推荐答案

它开始对我起作用,通过执行以下步骤,

  1. 确保将导入添加(CommonModule, MatIconModule, MatButtonModule)到要动态呈现的组件

  2. 使用setInput方法创建角分量所需的绑定,否则绑定将不适用于@Input

  3. 运行子组件的更改检测,以便使用componentRef.changeDetectorRef.detectChanges();检测组件更改

渲染组件

import { CommonModule } from '@angular/common';
import { Component, Input } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';

@Component({
  selector: 'app-table-action-container',
  standalone: true,
  imports: [CommonModule, MatIconModule, MatButtonModule],
  template: `
  <button mat-button color="primary" *ngIf="show">
    asdf
    <mat-icon color="primary"></mat-icon>
  </button>
  <button mat-button color="primary" *ngIf="!show">
    qwer
    <mat-icon color="primary"></mat-icon>
  </button>
  `,
  styleUrl: './table-action-container.component.scss',
})
export class TableActionContainerComponent {
  @Input() show = false;
}

指令

import {
  ApplicationRef,
  ComponentRef,
  Directive,
  ElementRef,
  Input,
  Renderer2,
  createComponent,
} from '@angular/core';
import { TableActionContainerComponent } from './table-action-container/table-action-container.component';

@Directive({
  selector: '[appTableActions]',
  standalone: true,
})
export class TableActionsDirective {
  @Input() show = false;
  constructor(
    private _appRef: ApplicationRef,
    private _renderer: Renderer2,
    private _elementRef: ElementRef
  ) {}

  ngOnInit() {
    const component = TableActionContainerComponent;
    let componentRef: ComponentRef<any> = createComponent(component, {
      environmentInjector: this._appRef.injector,
    });
    componentRef.setInput('show', this.show);

    this._renderer.appendChild(
      this._elementRef.nativeElement as HTMLElement,
      componentRef.location.nativeElement
    );
    componentRef.changeDetectorRef.detectChanges();
  }
}

html片段

  ...
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef>Action</th>
    <td
      mat-cell
      *matCellDef="let element;let index = index"
      appTableActions
      [show]="index % 2 === 0"
    ></td>
  </ng-container>
  ...

Stackblitz Demo

Angular相关问答推荐

无法绑定到ngIf,因为它不是dis angular 17的已知属性

RXJS运算符用于combineLatest,不含空值

MAT表的内联文本编辑-未刷新编辑图标

独立组件;依赖项注入

Angular KendoGrid RowReorderable,drop不适用于新添加的行

Angular 17不接受带点(.)的路径在开发环境中,它直接抛出一个404错误

具有多个输入的Angular struct 指令在第一次加载构件时没有值

如何捕获生命周期方法中抛出的Angular组件错误?

从 applescript 调用Angular 前端以从列表中 Select 一个项目

路由链接不在 Ionic 中执行

RxJS 基于先前的数据响应执行请求(涉及循环对象数组)

如何在插值中编写条件?

如何在angular 5的组件中格式化日期

如何使用 Bootstrap 4 flexbox 填充可用内容?

为什么我们需要`ngDoCheck`

在 RxJS Observable 中 flatten数组的最佳方法

Angular 2 中的 OnChanges 和 DoCheck 有什么区别?

Angular 5 测试:如何获取对子组件的引用

升级到 angular-6.x 会给出Uncaught ReferenceError: global is not defined

如何在 Angular 中使用 router.navigateByUrl 和 router.navigate