在Angular中使用ngValue选择默认选项

时间:2020-07-10 09:47:42

标签: arrays angular select default option

我已经阅读了很多有关该主题的文章,但我仍然不明白这段代码中发生了什么。

我正在尝试选择默认值(component.html):

    <select class="custom-select" formControlName="country">
          <option *ngFor="let country of countries$ | async" [ngValue]="country.myCode">{{ country.name }}</option>
        </select>

countries$导致Query.select('countries')

这会导致一个界面:


   export interface AppState {
     countries?: Country[];
   }

以及国家(地区)数组:

    export interface Country {
      id: number;
      name: string;
      myCode: string;
    }

我试图用[(ngModel)]="countries$"解决它,但是没有用。我还尝试踢出ngValue以使用[value],但每个国家/地区都包含省份,而我踢出ngValue后就会消失。

不知道该怎么办,请帮忙。谢谢

0 个答案:

没有答案
相关问题