onclick element on a mat table cell with expandable row



所以我正在开发可扩展的MatRow表,但我的问题是我需要在每行添加一个带有点击元素的按钮,并在点击时打开一个模态,但它不起作用

<table mat-table [dataSource]="dataSource" multiTemplateDataRows>
<ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
<th mat-header-cell *matHeaderCellDef > {{column}} </th>
<td mat-cell *matCellDef="let element" (click)="getTableElement(element)" [innerHtml]="element[column] | safeHtml"></td>

</ng-container>
<table>

我正在显示基于内部html的数据,并使用管道来净化和允许显示html元素,但我的点击功能不起作用

到目前为止我尝试了什么

事件绑定到按钮

elementRef.nativeElement.querySelector('a').addEventListener('click',alert(1))

要点击的Hostlistener id

@HostListener('document:click',['$event.target']) onDocumentClick(event:any){
alert(event.id)
}

但什么都不管用,我能知道我做错了什么吗?任何帮助都将不胜感激。

感谢您的回答,但我的代码中有一个错误。已更正

为什么不直接在HTML中的按钮中添加一个点击事件,比如:

<button (click)="doSomething()"></button>

您可以在此处阅读更多关于角度事件绑定的信息:https://angular.io/guide/event-binding

最新更新