本文介绍了Angular 2 ngModelChange选择选项,获取特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角度为2的下拉选择表单.

I have a dropdown select form in angular 2.

当前:当我选择一个选项时,option name被传递为$event

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

想要:当我选择一个选项时,我想将workout.id传递到我的onChange函数中.

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

我该如何实现?

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

推荐答案

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

选中此

这篇关于Angular 2 ngModelChange选择选项,获取特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 02:32