在下面的代码中,我的国家选择选项被触发了很多次,以至于浏览器停止响应。
<div [formGroup]="countryForm">
<mat-form-field>
<mat-select formControlName="selectedCountry" class="my-item-text" placeholder="Country">
<mat-option (onSelectionChange)="onCountrySelectionChanged($event)" *ngFor="let myCountry of countries" [value]="myCountry.short" class="my-item-text">{{ myCountry.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
我的组件代码如下
onCountrySelectionChanged(changeEvent) {
if (changeEvent && changeEvent.isUserInput && this.selectedCountry != changeEvent.source.value) {
this.countrySelected.emit( changeEvent.source.value);
}
}
我试图通过检查其用户更改事件 [isUserInput] 并检查值是否真的改变来进行限制!现在能够减少火灾事件并且应用程序正常工作。
有没有更好的方法来使用选择选项,因为现在包括上面的逻辑,到处都在使用 mat-select 组件。