Angular2反应形式选择多个属性? [英] Angular2 reactive forms select multiple attribute?
问题描述
我在我的应用中将以下代码用于响应式表单.
I use the following code in my app with reactive forms.
如果我取消注释[multiple]
行,则 Choose ... 选项不会将dformControl
表单控件对象设置回状态INVALID
.
If I uncomment the [multiple]
line, the Choose ... option does not set the dformControl
form control object back to status INVALID
.
dformControl.multiple
顺便返回false
.即使我将注释行更改为[multiple]="false"
,仍然切换回选择... 选项也不会将表单控件的状态设置为INVALID
.
dformControl.multiple
by the way returns false
. Even if I change the commented line to [multiple]="false"
, still switching back to the Choose ... option does NOT set the form control status to INVALID
.
<select class="form-control"
[id]="dformControl.key"
[formControlName]="dformControl.key"
/*[multiple]="dformControl.multiple"*/>
<option *ngIf="!dformControl.value"
value="">
Choose ...
</option>
<option *ngFor="let opt of dformControl.options"
[value]="opt.value"
[selected]="dformControl.value == opt.value">
{{opt.label}}
</option>
</select>
推荐答案
在选择级别绑定到布尔值isMultiple的multiple
属性.然后,您可以更改它,选择也将更改.看看这个,我用一个按钮来改变它. plnkr
Bind to the multiple
property at the select level to a boolean isMultiple. Then you can change it and the select will change as well. Take a look at the this, I change it using a button. plnkr
<select formControlName="cars" [multiple]="isMultiple">
<option></option>
<option *ngFor="let car of cars" >{{car}}</option>
</select>
似乎在添加多属性时会影响所需的验证器.我能够添加一个额外的验证器,它按预期工作.
It seems when adding the multiple property it is affecting the required validator. I was able to just add an extra validator and it worked as expected.
Validators.compose([Validators.required,Validators.pattern('.+')]
这篇关于Angular2反应形式选择多个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!