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?