在角4中,我在json配置文件中定义了以下配置。
countries: ['USA', 'UK', 'Canada'];
default: 'UK'我需要使用反应性模块在下拉列表中显示这些内容。
下面是执行此操作的代码(ts)
countries: string[] = [];
default: string;
...
this.countries = config.countries;
this.default = config.default;html
<select id="country" formControlName="country" >
<option *ngFor="let c of countries" [value]="c" >{{ c }}</option>
</select> 这样做的工作,并显示国家在一个下降。但是,我还需要在默认情况下选择一个国家,默认国家来自json中定义的“默认”键。
所以,我试着做这样的事
{{ c}
然而,这是行不通的。默认情况下,如果选中,则为空值。
如何确保在默认情况下选择预定义值?
发布于 2017-10-30 09:19:35
您必须创建一个新属性(例如:selectedCountry),并且应该在[(ngModel)]中使用它,并在组件文件中进一步使用它,将default值赋给它。
在your_component_file.ts中
this.selectedCountry = default;在your_component_template.html中
<select id="country" formControlName="country" [(ngModel)]="selectedCountry">
<option *ngFor="let c of countries" [value]="c" >{{ c }}</option>
</select> https://stackoverflow.com/questions/47011521
复制相似问题