在Angular 7中,当选择选项选中时,可能会触发两次事件。这可能是由于事件冒泡或事件委托导致的。下面是一种解决方法:
export class YourComponent {
optionSelected = false;
onSelect() {
if (!this.optionSelected) {
// 执行你需要的操作
console.log("选项被选择");
this.optionSelected = true;
} else {
this.optionSelected = false;
}
}
}
onSelect
方法:
通过这种方法,当选项被选择时,onSelect
方法将只会被触发一次。