[英]Angular 2 one way binding removing selected option of select tag
I have this code in my test.component.html file: 我的test.component.html文件中包含以下代码:
<select class="form-control input-sm">
<option value="" disabled selected>Supplier</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
It shows up nice like this: 它显示如下:
For some reason, when I add one-way binding from the template to the Component on that select tag the selected option disappears: 出于某种原因,当我从模板向该select标签上的Component添加单向绑定时,所选选项消失了:
<select (ngModel)="item.supplier" class="form-control input-sm">
<option value="" disabled selected>Supplier</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
What is causing this to happen and how to fix it ? 是什么原因导致这种情况发生以及如何解决?
I think it's disappearing because you're binding to the output
event for ngModelChange
but not actually using the value provided by the $event
. 我认为它正在消失,因为您将绑定到
ngModelChange
的output
事件,但实际上并未使用$event
提供的值。
This should work for you (note I assume there's an item object already initialised in the component). 这应该为您工作(请注意,我假设组件中已经初始化了一个项目对象)。
<select [ngModel]="''" (ngModelChange)="item.supplier = $event" class="form-control input-sm">
<option value="" disabled selected>Supplier</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
This is using ngModel
in the input and output bindings separately, rather than the two-way binding [(ngModel)]
- see here . 这是在输入和输出绑定中分别使用
ngModel
,而不是双向绑定[(ngModel)]
-参见此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.