Angular 如何还原特定选项的Mat Select选定值?
我正在使用mat select,我的要求是恢复mat select的mat选项的特定值的选择 例如,看看这个 这里mat select有三个选项;当我选择Canada时,它应该恢复到我以前选择的值Angular 如何还原特定选项的Mat Select选定值?,angular,typescript,angular-material,dropdown,Angular,Typescript,Angular Material,Dropdown,我正在使用mat select,我的要求是恢复mat select的mat选项的特定值的选择 例如,看看这个 这里mat select有三个选项;当我选择Canada时,它应该恢复到我以前选择的值 onCountryChange($event) { const oldIndex = this.countries.indexOf(this.selectedCountry); if ($event.value.short === 'CA') { this.select
onCountryChange($event) {
const oldIndex = this.countries.indexOf(this.selectedCountry);
if ($event.value.short === 'CA') {
this.selectedCountry = this.countries[oldIndex];
} else {
this.selectedCountry = $event.value;
}
}
我可以看到,除了mat select之外,json还更新了该值。但是,mat select不会更新所选选项
到目前为止,我尝试使用FormControl、setTimeout设置值,方法是只附加对象的short属性,而不是附加整个对象的mat选项以供参考。但无法使其工作。我认为您应该使用mat select触发器。
这是由于使用OnPush变化检测选择垫而造成的。您可能对使用mat select触发器感兴趣。试试看,让我知道它是否适合你
<mat-select name="countryVaraible" [value]="selectedCountry" placeholder="Country" (selectionChange)="onCountryChange($event)">
<mat-select-trigger>{{selectedCountry.full}}</mat-select-trigger>
<mat-option *ngFor="let country of countries" [value]="country">
{{country.full}}
</mat-option>
</mat-select>