Getting the Selected Object in Select Option in Angular

人走茶凉 提交于 2019-12-01 09:47:28

问题


I have this array of objects which I put on the select option and I would want to select one list of object in the select option. However I can't get the value of that list of object. It outputs [object] [Object]. I want to get that selected list of object of the corporation. I assigned it to the [value] and it gets [object][Object].

<div class="select" >
    <select class="form-control col-lg-8" #selectedValue (change)="assignCorporationToManage(selectedValue.value)">
        <option *ngFor="let corporation of user_corporations" [value]="corporation">{{corporation.corp_name}}</option>    
    </select>
</div>

ts

assignCorporationToManage(selectedValue) {
     console.log(selectedValue)
}

回答1:


Try like this :

add ngModel for your select and use ngModelChange instead of change.

  1. adding ngModel
  2. using ngModelChange instead of change.
  3. using [ngValue] instead of [value].

component.html

<select class="form-control col-lg-8" #selectedValue name="selectedValue" id="selectedValue" [(ngModel)]="selectedValue" (ngModelChange)="assignCorporationToManage($event)">
    <option *ngFor="let corporation of user_corporations" [ngValue]="corporation">{{corporation.corp_name}}</option>
</select>

Component.ts

assignCorporationToManage(selectedValue) {
     console.log(selectedValue)
}


来源:https://stackoverflow.com/questions/46536815/getting-the-selected-object-in-select-option-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!