简体   繁体   English

Angular2反应形式选择多个属性?

[英]Angular2 reactive forms select multiple attribute?

I use the following code in my app with reactive forms. 我在我的应用程序中使用以下代码与反应形式。

If I uncomment the [multiple] line, the Choose ... option does not set the dformControl form control object back to status INVALID . 如果我取消注释[multiple]行,则Choose ...选项不会将dformControl表单控件对象设置回状态INVALID

dformControl.multiple by the way returns false . dformControl.multiple顺便返回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 . 即使我将注释行更改为[multiple]="false" ,仍然切换回Choose ...选项不会将表单控件状态设置为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>

Bind to the multiple property at the select level to a boolean isMultiple. 绑定到选择级别的multiple属性为布尔值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 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('.+')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM