Angular 2:如何从表单的不同选项中获取选定的值? [英] Angular 2: How to get the selected value from different options of a form?

查看:24
本文介绍了Angular 2:如何从表单的不同选项中获取选定的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表单中使用 <option *ngFor="#type of types" [value]="type">{{type}}</option></选择>

组件.ts

this.types = [ 'type1', 'type2', 'type3' ];this.order = {类型:'type1'};呼叫类型(值){控制台日志(值);this.order.type=value;}

I would like to use a <select> in a form to let the user being able to update values among different <option>. I have used the technique from the guide here: https://angular.io/docs/ts/latest/guide/forms.html. Here is the sample I am talking about:

<div class="form-group">
    <label for="type">Type :</label>
    <select class="form-control" [(ngModel)]="order.type" ngControl="type">
        <option *ngFor="#type of types" [value]="type">{{type}}</option>
    </select>
</div>

In my order-details.component I have got an updateOrder() which calls the updateOrder() from myApp.services.

My problem is when I am trying to send the data from the form to the back-end: all the parts with an <input> are OK, but not those with <select> (it returns the original values, and not the one selected).

Does anyone have encountered this or a similar problem? Thanks for your help!

解决方案

There is a way to get the value from different options. check this plunker

component.html

 <select class="form-control" #t (change)="callType(t.value)">
      <option *ngFor="#type of types" [value]="type">{{type}}</option>
    </select>

component.ts

this.types = [ 'type1', 'type2', 'type3' ];
   this.order = {
      type: 'type1'          
  };  

  callType(value){
    console.log(value);
    this.order.type=value;
  }

这篇关于Angular 2:如何从表单的不同选项中获取选定的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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