如果未选中复选框,如何在打字稿中构建函数?

时间:2019-06-19 13:25:47

标签: angular typescript

所以我需要创建此函数:

  buildForm(): void {

    console.log('build form');
    this.notificationForm = this.fb.group({
      appCreated: [],
      appSubmittedReview: [],
      appCancelReview: [],
      appRequestForDeletion: [],
      appDisable: [],
      appReEnable: [],
      appReviewPublished: [],
      appReviewApproved: [],
      appReviewDeleted: [],

    });
  }

所有字段都是一个复选框,我希望默认情况下不选中它们,任何人都可以帮助我创建此buildForm。 预先感谢

2 个答案:

答案 0 :(得分:1)

使用false作为默认值

buildForm(): void {

    console.log('build form');
    this.notificationForm = this.fb.group({
      appCreated: false,
      appSubmittedReview: false,
      appCancelReview: false,
      appRequestForDeletion: false,
      appDisable: false,
      appReEnable: false,
      appReviewPublished: false,
      appReviewApproved: false,
      appReviewDeleted: false,

    });
}

答案 1 :(得分:1)

该字段的状态取决于FormBuilder的默认值,请尝试以下操作:

  this.notificationForm = this.fb.group({
      appCreated: [true],
      appSubmittedReview: [],
      appCancelReview: [],
      appRequestForDeletion: [],
      appDisable: [true],
      appReEnable: [],
      appReviewPublished: [],
    });

以HTML

   <form [formGroup]="notificationForm" (ngSubmit)="onSubmit()">
         appCreated:  <input type="checkbox" formControlName="appCreated" >
         appSubmittedReview:  <input type="checkbox" formControlName="appSubmittedReview" >
         appCancelReview:  <input type="checkbox" formControlName="appCancelReview" >
         appRequestForDeletion:  <input type="checkbox" formControlName="appRequestForDeletion" >
         appDisable:  <input type="checkbox" formControlName="appDisable" >
         appReEnable:  <input type="checkbox" formControlName="appReEnable" > 
      </form>

Working Demo

相关问题