4

我的项目中有 2 个对象:acompanyuser. 我有一个表格,用户可以在其中更新他的个人资料。他可以更新的一件事是国家。现在要显示我使用下拉列表的国家/地区。

我想selected在选项中设置属性,其中 的name等于country实际countryuser。( country.name==user.country)

这是我尝试过的,但它似乎不起作用。

<select>
    <option *ngFor="#c of countries" [ngStyle]="setStyles()" [ngValue]="c">{{c.name}}</option>          
</select>

setStyles(){
    let styles;
    if (this.companies[i].name == this.user.country ) {
        styles = {
            'selected'
        } 
    return styles;
}
4

1 回答 1

8

我会尝试以下方法:

<select>
  <option *ngFor="#c of countries"
       [attr.selected]="c.name == user.country ? true : null" 
       [ngValue]="c">
    {{c.name}}
  </option>
</select>

我认为这是您需要的属性而不是样式。

于 2016-05-17T07:24:22.920 回答