带有primeng的Angular2复选框失败

时间:2017-01-30 04:53:13

标签: angular typescript checkbox

我正在使用angular2 cli,这是我设置表单的方式

在组件中

export class UsersAddComponent implements OnInit {

 ngOnInit() {
    this.userForm = this._formBuilder.group({
      role: ['', [Validators.required]],
      others: this._formBuilder.array([])  //for adding multipe form inputs
    });

   this.addNewUser();

  }

  initAddress() {
     return this._formBuilder.group({     
       sendcred: [''],  //checkbox needs no validation in my logic
        needs_reset: [''], // ''
        .....other fields here
    });
 }


  addNewUser() {  //this is called whenever add new user button is clicked
     const control = <FormArray>this.userForm.controls['others'];
     const addrCtrl = this.initAddress();
     control.push(addrCtrl);
   }

在html模板中我正在使用像这样的primeng复选框

  <p-checkbox formControlName="needs_reset" label="User has to set password" 
   (onChange)="Onpwdchange()"></p-checkbox>

  <p-checkbox formControlName="sendcred" name="send cred"  label="Send user
 login credentials " (onChange)="Oncredchange()"></p-checkbox>

方法onpwdchange()和oncredchange()只有一个console.log("clicked")

每当我检查复选框时都会收到错误

this.model.push is not a function  //i havent implemented push method anywhere

我已查看This primeng2 issue,但他们建议使用{2}中无法使用的disableDeprecatedForms()provideForms()

我如何解决这个问题

2 个答案:

答案 0 :(得分:2)

  1. 按如下方式初始化表单值
  2. file.ts

    this.infraestructuraServiciosForm = this.fb.group({
         ....
         complementarios : new FormControl('', []),
         ......
    });
    
    1. 以下行可避免错误:

      this.infraestructuraServiciosForm.controls['complementarios'].setValue([]);
      
    2. file.html

      <p-fieldset legend="Complementarios">
                  <body>
                      <div class="ui-g ui-fluid">
                           <div class="ui-g-12">
                              <div class="ui-g-3">
                                  <p-checkbox name="cpl" value="acerasBordillos" label="Aceras y Bordillos" formControlName="complementarios" (onChange)="haCambiadoAcerasBordillos(checked)"></p-checkbox>
                              </div>
                              <div class="ui-g-3">
                                  <p-checkbox name="cpl" value="soloBordillos" label="Solo Bordillos" formControlName="complementarios" (onChange)="haCambiadoSoloBordillos(checked)"></p-checkbox>         
                              </div>
                              <div class="ui-g-3">
                                  <p-checkbox name="cpl" value="telefonoFijo" label="Red de Teléfono Fijo" formControlName="complementarios"></p-checkbox>      
                              </div>          
                              <div class="ui-g-3">
                                  <p-checkbox name="cpl" value="alumbradoPublico" label="Alumbrado Público" formControlName="complementarios"></p-checkbox>                              
                              </div>
                          </div>                                     
                      </div>
                  </body>
              </p-fieldset>
      

答案 1 :(得分:2)

根据PrimeNG文档

https://www.primefaces.org/primeng/#/checkbox

formControlName不能与primeNG的复选框一起使用。

所以你需要这样做

<p-checkbox value="xyz" [formControl]="myFormGroup.controls['name']"></p-checkbox>

如果你想要二进制值true / false

<p-checkbox binary="true" [formControl]="myFormGroup.controls['name']"></p-checkbox>