角材料通过使用表单组和表单数组获取选中复选框的值

时间:2018-08-23 13:36:12

标签: checkbox angular-material

我要生成带有数组的复选框

  

component.ts

profiles: string[] = [
    'Sales Admin',
    'Finance Admin',
    'Gateway Admin',
    'Support Admin',
  ];
profileControls: any;

//creating formcontrols
constructor() {    
    this.profileControls = this.profiles.map(c => new FormControl(false));
    this.createFormControls();
    this.createForm();
  }

createFormControls() {    
    this.userid = new FormControl('', Validators.email);
    this.accsprofile = new FormArray(this.profileControls);
}

createForm() {
    this.myform = new FormGroup({      
      userid: this.userid,      
      accsprofile: this.accsprofile
    });
  }
  

component.html

<div *ngFor="let profile of profileControls let i=index" >
    <mat-checkbox class="example-margin" [formControlName]="profiles[i]">
      {{profiles[i]}}
    </mat-checkbox>
</div>

尽管我选中了所有复选框,但输出显示为false。我想获取已选中复选框的值

  

输出   enter image description here

0 个答案:

没有答案