角2:如何从一种形式的不同选择所选择的价值呢? [英] Angular 2: How to get the selected value from different options of a form?
问题描述
我想用一个<的形式
来让用户能够之间不同的更新值<选择&GT选项>
。我用从这里导向的技术: https://angular.io/docs /ts/latest/guide/forms.html 。这是我讲的样本:
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>
在我的订单details.component我有从myApp.services调用updateOrder()的updateOrder()。
In my order-details.component I have got an updateOrder() which calls the updateOrder() from myApp.services.
我的问题是,当我试图从形式到后端发送数据:所有的&LT的部分;输入&GT;
都OK,但不那些&LT;选择方式&gt;
(它返回原来的价值,而不是一个选择)
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!
推荐答案
有一种方法可以从不同的期权价值。
检查这个 plunker
There is a way to get the value from different options. check this plunker
component.html
component.html
<select class="form-control" #t (change)="callType(t.value)">
<option *ngFor="#type of types" [value]="type">{{type}}</option>
</select>
component.ts
component.ts
this.types = [ 'type1', 'type2', 'type3' ];
this.order = {
type: 'type1'
};
callType(value){
console.log(value);
this.order.type=value;
}
这篇关于角2:如何从一种形式的不同选择所选择的价值呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!