如何解决如何针对特定选项还原Mat-Select的选定值?
我正在使用mat-select
,我的要求是将选择恢复为mat-select
的{{1}}的特定值。
例如,看看这个stackblitz
mat-option
这里有三个选项;当我选择mat-select
时,它应该恢复为我先前选择的值。
Canada
我看到除了 onCountryChange($event) {
const oldIndex = this.countries.indexOf(this.selectedCountry);
if ($event.value.short === 'CA') {
this.selectedCountry = this.countries[oldIndex];
} else {
this.selectedCountry = $event.value;
}
}
之外,json
的值也正在更新。但是,mat-select
不会更新所选的选项。
到目前为止,我尝试使用mat-select
,FormControl
设置值,方法是仅附加对象的setTimeout
属性,而不是使用short
({{3} } 以供参考)。但是无法使其正常工作。
解决方法
我认为您应该使用 mat-select-trigger 。 这是由于使用OnPush更改检测进行了垫选择。您可能对使用 mat-select-trigger 感兴趣。试试看,让我知道它是否适合您。
<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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。