首页 > 解决方案 > 为什么我在 ng-template 中双击?

问题描述

我有以下模板:

<table class="table table-borderless tablelist-custom-style">
 <tbody>
    <ng-container [ngTemplateOutlet]="ordersTableHeadTemplate"></ng-container>
</tbody>
</table>

<ng-template #ordersTableHeadTemplate>
     <tr>
     <th class="align-bottom">
        <div class="custom-control custom-checkbox" (click)="test()">
     </th>
     </tr>
</ng-template>

当我这样做时,(click)="test()"我得到了两次点击而不是一次:

在此处输入图像描述

方法是:

 test() {
     console.log('1');
 }

标签: angular

解决方案


NgTemplateOutlet是一个结构指令,用 asterix 调用。尝试相应地调用它:

<ng-container *ngTemplateOutlet="ordersTableHeadTemplate"></ng-container>

推荐阅读