3

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

2 Answers 2

7

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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

change into this [(ngModel)]="selectedValue[i]"
0

you can get that particular object from the list by using selectedValue.

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

in your ts or js file,

const selectedObject = this.user_corporations.find((el: any) => {
  return el?.corporation == this.selectedValue;
});

After that, you can use the selectedObject whatever you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.