Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Angular 2 ngModelChange select option, grab a specific property

I have a dropdown select form in angular 2.

Currently: When I select an option the option name gets passed into my onChange function as $event

Wanted: When I select an option I would like to pass workout.id into my onChange function.

How can I achieve that?

<select class="form-control" [ngModel]="selectedWorkout" (ngModelChange)="onChange($event)">
    <option *ngFor="#workout of workouts">{{workout.name}}</option>
</select>

Controller

onChange(value){
  alert(JSON.stringify(value));
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

<select class="form-control" [ngModel]="selectedWorkout" (ngModelChange)="onChange($event)">
    <option *ngFor="#workout of workouts" [value]="workout.id">{{workout.name}}</option>
</select>

OR

<select class="form-control" [(ngModel)]="selectedWorkout" (change)="onChange($event.target.value)">
    <option *ngFor="#workout of workouts" [value]="workout.id" >{{workout.name}}</option>
</select>

check this

over 4 years ago · Santiago Trujillo Report

0

you could also use [ngValue] inside option:

<select class="form-control" [ngModel]="selectedWorkout" (ngModelChange)="onChange($event)">
  <option *ngFor="#workout of workouts" [ngValue]="workout">{{workout.name}}</option>
</select>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!