[英]Angular 2 select does not bind
I played with this for quite awhile, selection list shows okey, but I cannot get it to display the selected value - it is empty. 我玩了一段时间,选择列表显示为okey,但是我无法显示选择的值-它为空。 What am I doing wrong?
我究竟做错了什么?
<div class="form-group"> <label for="ai_total_volume_select">Select Volume Range:</label> <select class="form-control" [(ngModel)]="selectedValue" name="items" id="ai_total_volume_select"> <option [value]="item1">Select</option> <option [value]="item2">< 0.5 cm3</option> <option [value]="item3">> 0.5 cm3</option> </select> </div> <label>AAA</label> <div>{{selectedValue}}</div>
EDIT: I played a bit more and now know where the problem is coming from, but have no clue how to fix it. 编辑:我玩了更多,现在知道问题出在哪里,但不知道如何解决。 This select field is within:
该选择字段位于:
<form [formGroup]="form">
. 。 And somehow it does not like it.
而且它不喜欢它。 Once I move it outside the form - it works!
一旦将其移出表格,它就会起作用! But I need it to be within the form.
但是我需要它在表格中。 Other fields - checkboxes, inputs etc work perfectly fine in the same form...
其他字段-复选框,输入等在相同形式下也可以正常工作...
You messed up a little bit with value you are passing empty object. 您将传递空对象的值弄乱了一点。 Add ' ' and it will work created plunker .
添加''它将创建创建的插件 。
<select class="form-control" [(ngModel)]="selectedValue" name="items" id="ai_total_volume_select">
<option [value]="'item1'">Select</option>
<option [value]="'item2'">< 0.5 cm3</option>
<option [value]="'item3'">> 0.5 cm3</option>
</select>
or use value without [] 或使用不带[]的值
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
<select class="form-control" [(ngModel)]="selectedValue" name="items" id="ai_total_volume_select">
<option value="item1">Select</option>
<option value="item2">< 0.5 cm3</option>
<option value="item3">> 0.5 cm3</option>
</select>
</form>
for Reactive Forms use FormControlName 对于反应式使用FormControlName
<form [formGroup]="form">
<select class="form-control" formControlName="selectedValue" name="items" id="ai_total_volume_select">
<option value="item1">Select</option>
<option value="item2">< 0.5 cm3</option>
<option value="item3">> 0.5 cm3</option>
</select>
<form>
{{form.controls.get('selectedValue').value}}
For better understanding template syntax read this article 为了更好地理解模板语法,请阅读本文
Unless the item
values are defined in your component you don't need the [value]
binding. 除非在组件中定义了
item
值,否则不需要[value]
绑定。
You can use standard html here: 您可以在此处使用标准html:
<option value="item1">Select</option>
<option value="item2">< 0.5 cm3</option>
<option value="item3">> 0.5 cm3</option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.