Getting object properties via ngModel select list in Angular 2?

前端 未结 2 2121
情歌与酒
情歌与酒 2020-12-15 19:23

I\'m have a problem fetching the properties of an object which has been selected from a select list in Angular 2 (RC1). Take the following syntax:


                        
    
提交评论

  • 2020-12-15 20:10

    To use objects as value use [ngValue] instead of [value]. [value] only supports string ids.

    <select required [(ngModel)]="model"> <!-- <== changed -->
      <option selected="selected" disabled>Plan...</option>
      <option *ngFor="#plan of plans" [ngValue]="plan">{{ plan.name }}</option>
    </select>
    

    Plunker example

    model needs to point to one of the elements in plans to work as default value (it needs to be the same instance, not another instance containing the same values).

    0 讨论(0)
  • 提交回复
    热议问题