【问题标题】:using formGroup when template reference variable is used throws an error使用模板引用变量时使用 formGroup 会引发错误
【发布时间】:2018-02-28 00:31:28
【问题描述】:

我有一个带有模板引用变量的现有表单。现在,当我想添加 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" 抛出错误“无法读取属性 'valid' of null”。此错误仅在我使用[formGroup]="formGroup"时显示

【问题讨论】:

  • 你的 ts 文件中的 formGroup 是什么?

标签: html angular forms angular-forms


【解决方案1】:

试试这个

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多