使用模板引用变量时使用 formGroup 会引发错误

using formGroup when template reference variable is used throws an error

我有一个带有模板引用变量的现有表单。现在,当我想添加 formGroup 指令时,模板引用变量的有效 属性 会引发错误。

<form (ngSubmit)="createTask()"
      [formGroup]="formGroup"
      id="taskFormId"
      autocomplete="off"
      #taskForm="ngForm">
// my code
</form>

<button type="submit"
          form="taskFormId"
          mat-raised-button
          color="primary"
          [disabled]="!taskForm.form.valid">
    Submit
  </button>

[disabled]="!taskForm.form.valid" 引发错误 "Cannot read property 'valid' of null"。此错误仅在我使用 [formGroup]="formGroup"

时显示

试试这个

<form (ngSubmit)="createTask()"
      [formGroup]="yourFormGroup"
      id="taskFormId"
      autocomplete="off"
      #taskForm="ngForm">
// my code
</form>

<button type="submit"
          form="taskFormId"
          mat-raised-button
          color="primary"
          [disabled]="!yourFormGroup.get('yourControlName').valid">
    Submit
  </button>