Angular 2如何将Dropdownlist中的选定ID与模型绑定? [英] Angular 2 How to bind selected id in Dropdownlist with model?

查看:73
本文介绍了Angular 2如何将Dropdownlist中的选定ID与模型绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在选择Dropdownlist的某个值时调用该函数,并且该函数将使用所选值的ID.如何将所选值的ID绑定到模型?我可以将数据加载到下拉列表中.

I want to call the function when some value of Dropdownlist is selected and that function will use the selected value's id. How could i bind the selected value's id to model? I can load data to the dropdownlist.

<select>
  <option *ngFor="let category of categorylist" >
             {{ category.Description }}</option>
                     </select>

推荐答案

可以自定义angular2中的2种方式绑定,我们可以调用自己的函数.只需将ID绑定到 ngValue 上,然后将该值放在 DropdownChange 事件上即可.

2 way binding in angular2 can be customized and we can call upon our own function. Just bind the id to the ngValue and have that value go on the DropdownChange event.

HTML:

<select [ngModel]="selectedValue" (ngModelChange)="onDropdownChange($event)" class="form-control">
        <option *ngFor="let category of categorylist" [ngValue]="category.id">{{category.Description}}</option>
     </select>  

组件:

onDropdownChange(e){
  console.log(e)//you will get the id  
  this.selectedValue =e //if you want to bind it to your model
}

这篇关于Angular 2如何将Dropdownlist中的选定ID与模型绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆