动态如何在单击的表单元格中创建组件

发布于 2025-02-11 02:59:35 字数 909 浏览 2 评论 0原文

some.component.html

<table>
  <tr *ngfor="let row in table">
    <td *ngFor="let cell in row" (click)="onCellClick($event)">Cell Text</td>
  </tr>
</table>
@Component({
  selector: 'app-my-component',
  template: '<div>My Component</div>',
})
export class MyComponent { }


@Component({
  selector: 'app-some',
  templateUrl: './some.component.html'
})
export class SomeComponent {
  public table = [[1, 2, 3], [4, 5, 6]];

  constructor(
    private viewContainerRef: ViewContainerRef
  ) { }

  onCellClick(event: MouseEvent): void {
    const myComponent = this.viewContainerRef.createComponent(MyComponent);
  }
}

此代码在&lt;/table&gt;之后添加myComponent。但是我需要在单击的表单元格中创建myComponent(内部td)。我该怎么做?

some.component.html

<table>
  <tr *ngfor="let row in table">
    <td *ngFor="let cell in row" (click)="onCellClick($event)">Cell Text</td>
  </tr>
</table>
@Component({
  selector: 'app-my-component',
  template: '<div>My Component</div>',
})
export class MyComponent { }


@Component({
  selector: 'app-some',
  templateUrl: './some.component.html'
})
export class SomeComponent {
  public table = [[1, 2, 3], [4, 5, 6]];

  constructor(
    private viewContainerRef: ViewContainerRef
  ) { }

  onCellClick(event: MouseEvent): void {
    const myComponent = this.viewContainerRef.createComponent(MyComponent);
  }
}

This code adds MyComponent after </table>. But I need MyComponent to be created inside of the clicked table cell (inside td). How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2025-02-18 02:59:35

注意:此无工厂版本仅在每行中都有相同数量的单元格(因为我们使用CellsPerrow来猜测单元格的索引):

好的,首先导入所有我们需要的东西:

import {
       Component,
       OnInit,
       ViewContainerRef,
       QueryList,
       ViewChildren,
} from '@angular/core';

添加ViewChildren Decorator在构造函数下方以获取所有单元格的元素:

@ViewChildren('cellElem', { read: ViewContainerRef })cellsElemsList: QueryList<ViewContainerRef>;

添加一个cellperrow常数:

private cellPerRow = 3;

然后像这样修改您的oncellClick:

onCellClick = (rowIdx, colIdx) => {
    const cellIdx = (rowIdx * this.cellPerRow) + colIdx;
    this.cellsElemsList
      .filter((el, idx) => idx === cellIdx)[0]
      .createComponent(TestComponent);
  };

最后,更改HTML以将当前单元格的索引传递到OnCellClick()(并添加未来组件的容器,#cellelem,#cellelem,#cellelem,#cellelem, TD Elem):

<div *ngFor="let row of rows; let rowIdx = index;">
    <td *ngFor="let cell of row; let colIdx = index" (click)="onCellClick(rowIdx, colIdx)">
        <div #cellElem></div>
        Click me
    </td>
</div>

Note: This factory-less version only works if you have the same number of cells in every row (since we're using cellsPerRow to guess the index of the cell) :

Alright, first import everything we'll need :

import {
       Component,
       OnInit,
       ViewContainerRef,
       QueryList,
       ViewChildren,
} from '@angular/core';

Add a ViewChildren decorator below your constructor to get all the cells elems :

@ViewChildren('cellElem', { read: ViewContainerRef })cellsElemsList: QueryList<ViewContainerRef>;

Add a cellsPerRow constant :

private cellPerRow = 3;

Then modify your onCellClick like so :

onCellClick = (rowIdx, colIdx) => {
    const cellIdx = (rowIdx * this.cellPerRow) + colIdx;
    this.cellsElemsList
      .filter((el, idx) => idx === cellIdx)[0]
      .createComponent(TestComponent);
  };

Finally, change the HTML to pass the current cell's index to onCellClick() (and add the future component's container, #cellElem, inside your td elem) :

<div *ngFor="let row of rows; let rowIdx = index;">
    <td *ngFor="let cell of row; let colIdx = index" (click)="onCellClick(rowIdx, colIdx)">
        <div #cellElem></div>
        Click me
    </td>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文