无法在 Angular 5 中设置选定的值

用户2304483

我有以下 html 选择代码,我可以在其中看到国家/地区列表但看不到所选国家/地区(而是选择列表中的第一项):

<select class="selectpicker" [(ngModel)]="trip.country" (ngModelChange)="onSelectCountry($event)">
        <option *ngFor="let c of countries" [ngValue]="c.country">{{c.country.name}}</option>
    </select>

组件 ts 文件:

export class Trip1Component implements OnInit {

  @Input() public trip: Trip;
  public countries: Country[] = [];
  private selectedCountry: Country;

  constructor(private http: HttpClient,
    @Inject(LOCAL_STORAGE) private storage: WebStorageService) {
    this.countries = this.storage.get("countries");
  }

  ngOnInit() {
    this.onSelectCountry(this.trip.country);
    setTimeout(function () {
      $('.selectpicker').selectpicker(); 
    }, 10);

  }

onSelectCountry(val)  {
    this.selectedCountry = val;
  }
...
}

控制台中的国家/地区列表:

控制台中的国家/地区列表

旅行中的国家对象:

旅行中的国家对象

您能否建议应该在代码中更改哪些内容才能正确查看所选值?

萨吉塔兰

你的 ngValue 应该只是 c

 <option *ngFor="let c of countries" [ngValue]="c">{{c.country.name}}</option>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章