2016-01-22 34 views
8

我想在表单中使用<select>以让用户能够更新不同<option>之间的值。我在这里使用了指南中的技术:https://angular.io/docs/ts/latest/guide/forms.html。这是我讲的样品:它要求从myApp.services的updateOrder()Angular 2:如何从表单的不同选项中获取选定的值?

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

在我的订单details.component我有一个updateOrder()。

我的问题是当我试图将数据从表单发送到后端:<input>的所有部分都可以,但不是那些与<select>(它返回原始值,而不是所选的一个)。

有没有人遇到过这样或类似的问题? 感谢您的帮助!

+0

看起来像一个dup http://stackoverflow.com/questions/34686855/angular2-access-a-select-event-change-within-the-component –

回答

20

有一种方法可以从不同的选项中获取值。 检查这个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; 
    } 
1

其实我不能重现你的问题。我用inputselect以非常简单的形式创建了一个plunkr。当我提交表单时,我在绑定对象中有实际值。看到这个plunkr:https://plnkr.co/edit/5C3agW7QZfcrdt88QzSh?p=preview

随意告诉我,如果我没有正确理解你的问题。

Thierry

+0

其实,似乎你已经转载我的问题在你的plunkr:当我尝试提交表单时,我在''内获得了正确的值,但无论我在'