简体   繁体   中英

Getting selected value dropdown Angular 2

struggling a bit to get selected value from drop down.

As it is at the moment a value of undefined is 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);
}

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

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

               }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM