【发布时间】:2021-10-24 22:05:29
【问题描述】:
我的组件中有这个按钮工具:
<mat-button-toggle-group appearance="legacy">
<mat-button-toggle *ngFor="let seance of tableauSeances!">
<fa-icon icon="calendar-alt"></fa-icon>
{{ seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon icon="check-circle" class="check" *ngIf="verifierSeanceExistante(seance)"
(click)="onClickSeance(seance)"></fa-icon>
</mat-button-toggle>
</mat-button-toggle-group>
一切正常(tableauSeances 包含我的对象、fa-icons 和我的日期正确显示,..)但是当我点击我的按钮时,什么都没有:功能“onClickSeance”没有到达。
如果我把函数调用放在mat-button-toogle元素中,也是一样的……
但是,如果我在 mat-button-toogle-group 组之外调用相同的函数,则会调用该函数
你知道为什么点击不被捕获吗?
编辑 1
看到Eliseo回归后代码改变
TS:
clicSurUneSeance(event: MatButtonToggleChange): void {
// eslint-disable-next-line no-console
console.log(event.value);
}
HTML:
<div class="container-fluid">
<div *ngIf="saisonSelectionneeCalendrier && saisonSelectionneeCalendrier.id && this.verifierOptionActive()" class="row calendriers">
<mat-button-toggle-group appearance="legacy">
<mat-button-toggle *ngFor="let seance of tableauSeances!"
(change)="clicSurUneSeance($event)">
<fa-icon icon="calendar-alt"></fa-icon>
{{ seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon *ngIf="verifierSeanceExistante(seance)" class="check" icon="check-circle"></fa-icon>
</mat-button-toggle>
</mat-button-toggle-group>
</div>
</div>
编辑 2
ngFor中使用的数组结构
{ seance: ISeance,
selection: boolean,
identifiant: string
}[]
编辑 3
我尝试在同一个组件中不使用 ngFor。第一组不工作,但第二组是... ngFor 有错误?也许是错过了 ngFor 没有初始化并扰乱组的某些东西、属性或值?
<mat-button-toggle-group multiple="true">
<mat-button-toggle (change)="clicSurUneSeance($event)" *ngFor="let seancePossible of tableauSeancesPossibles!">
<fa-icon class="calendar" icon="calendar-alt"></fa-icon>
{{ seancePossible.seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon *ngIf="seancePossible.selection" class="check" icon="check-circle"></fa-icon>
</mat-button-toggle>
</mat-button-toggle-group>
<mat-button-toggle-group multiple="true">
<mat-button-toggle (change)="clicSurUneSeance($event)" [value]="tableauSeancesPossibles![0].seance">
<fa-icon class="calendar" icon="calendar-alt"></fa-icon>
{{ tableauSeancesPossibles![0].seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon *ngIf="tableauSeancesPossibles![0].selection!" class="check" icon="check-circle"></fa-icon>
</mat-button-toggle>
<mat-button-toggle (change)="clicSurUneSeance($event)" [value]="tableauSeancesPossibles![1].seance">
<fa-icon class="calendar" icon="calendar-alt"></fa-icon>
{{ tableauSeancesPossibles![1].seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon *ngIf="tableauSeancesPossibles![1].selection!" class="check" icon="check-circle"></fa-icon>
</mat-button-toggle>
<mat-button-toggle (change)="clicSurUneSeance($event)" [value]="tableauSeancesPossibles![2].seance">
<fa-icon class="calendar" icon="calendar-alt"></fa-icon>
{{ tableauSeancesPossibles![2].seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon *ngIf="tableauSeancesPossibles![2].selection!" class="check" icon="check-circle"></fa-icon>
</mat-button-toggle>
<mat-button-toggle (change)="clicSurUneSeance($event)" [value]="tableauSeancesPossibles![3].seance">
<fa-icon class="calendar" icon="calendar-alt"></fa-icon>
{{ tableauSeancesPossibles![3].seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon *ngIf="tableauSeancesPossibles![3].selection!" class="check" icon="check-circle"></fa-icon>
</mat-button-toggle>
<mat-button-toggle (change)="clicSurUneSeance($event)" [value]="tableauSeancesPossibles![4].seance">
<fa-icon class="calendar" icon="calendar-alt"></fa-icon>
{{ tableauSeancesPossibles![4].seance.dateSeance!.format('dddd D MMMM YYYY') }}
<fa-icon *ngIf="tableauSeancesPossibles![4].selection!" class="check" icon="check-circle"></fa-icon>
</mat-button-toggle>
</mat-button-toggle-group>
【问题讨论】:
-
这能回答你的问题吗? Change or click event of mat-button-toggle
-
是和不是。事实上, Eliséo 的回答和你的回答都很好,但由于某种我不知道的原因,它在 Stackblitz 上适用于我,但在我的 intelliJ 上却不适用。我正在检查我的模块的版本...
-
好吧,我没看到……我的 @angular/core 在 11.2.7 中,我的 @angular/material 在 11.2.13 中。没有 ngFor,它可以工作,但使用 ngFor,什么都不会发生(没有拦截点击,也没有改变按钮的视觉状态)......虽然在 stackblitz 上没问题!唯一的区别是我在 @angular/core 中是在 12.2.2 中……我真的不明白为什么……如果你有线索,我很感兴趣!
-
我在 EDIT 3 上有一个例子
标签: angular typescript