Search code examples
arraysangularjavascript-objectsangular-services

Getting the Selected Object in Select Option in Angular


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)
}

Solution

  • 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)
    }