表单中怎么获取复选框的值?

初始化的时候创建表单

ngOnInit() {
    this.retrieve();
    this.createForm();
  }
// 创建表单
  createForm() {
    this.form = this.formBuilder.group({
      funcAllID: new FormControl(),
    });
  }

通过触发进行keys更新,此处能够打印选中的keys值,但是funcAllId没有值

// 点击复选框触发
  nzCheck(event: NzFormatEmitEvent): void {
    this.keys = event.keys || [];
    this.form.patchValue({funcAllId: this.keys});
    console.log(this.keys);
  }

传递出去的值 ↓
image.png
HTML

<form nz-form [formGroup]="form" class="text-5">
  <nz-form-item>
    <nz-form-label [nzRequired]="true" [nzFor]="'funcAllID'">功能列表</nz-form-label>
    <nz-form-control nzHasFeedback>
      <nz-tree #nzTreeComponent *ngIf="testTree.length>0" [nzData]="testTree" nzCheckable [nzCheckedKeys]="keys"
        [nzExpandedKeys]="defaultExpandedKeys"(nzCheckBoxChange)="nzCheck($event)">
      </nz-tree> 
    </nz-form-control>
  </nz-form-item>
</form>

怎么样才能给后端传递最新的keys值?

阅读 2.5k
1 个回答

this.form.patchValue({funcAllId: this.keys});
创建表单funcAllID字段跟赋值字段funcAllId不一致,赋值不会报错,但是也不会成功

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题