Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何还原特定选项的Mat Select选定值?_Angular_Typescript_Angular Material_Dropdown - Fatal编程技术网

Angular 如何还原特定选项的Mat Select选定值?

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

我正在使用mat select,我的要求是恢复mat select的mat选项的特定值的选择

例如,看看这个

这里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;
    }
  }
我可以看到,除了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>