1

struggling a bit to get selected value from drop down.

As it is at the moment a value of undefinedis returned.

How can i get this selected value(opl.Opl_Id) in Angular 2?

<form class="form-inline" novalidate>
    <select class="form-control" (change)="onChange(opl)">
          <option [selected] = "opl.OplDescription == selectedOpl"  *ngFor="let opl of existingOpls" [ngValue]="opl.Opl_Id">{{opl.OplDescription}}</option>
    </select

//component
 onChange(value) {
    console.log(value);
}

2 Answers 2

2

Why do you call extra change event to just get the selected value??? Use [(ngModel)] to get updated value instead as shown below,

<select class="form-control" [(ngModel)]="selectedVal">              //<<<---here

      <option [attr.selected] = "opl.OplDescription == selectedOpl" //<<<---here
              *ngFor="let opl of existingOpls" 
              [ngValue]="opl.Opl_Id">
              {{opl.OplDescription}}
      </option>

</select>

{{selectedVal}}
Sign up to request clarification or add additional context in comments.

Comments

1

I found this easier.. Hope it helps future developers.

 <select class="form-control" id="select" (change)="Selected($event.target.value)">
        <option *ngFor="let item of items" [value]="item.id">{{item.value}}</option>
    </select>


    Selected(value: any) {

            console.log(value);

               }

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.