typescript 错误:找不到名称为的控件:带有表单生成器和表单组的“billDate”

vybvopom  于 2023-03-13  发布在  TypeScript
关注(0)|答案(1)|浏览(145)

我一直在尝试用formBuilder和formGroup创建一个表单,这是 typescript 文件:

constructor(
    private _userQueries: UserQueries,
    private _formBuilder: FormBuilder,
  ) { }

  ngOnInit(): void {
    this._userQueries.getCustomer().subscribe(({ data }) => {
      if (data) {
        this.customer = data.customer;

        this.updateInvoiceDates = this._formBuilder.group({
          billDate: [ this.customer.invoiceDates?.billDate, [Validators.required]],
          dueDate: [ this.customer.invoiceDates?.dueDate, [Validators.required]]
        });
      }
    });
  }

在模板中是这样的:

<form class="form" [formGroup]="updateInvoiceDates">
  <span class="subtitle">A continuación puedes definir las fechas de pago:</span>
  <div class="options">
    <span>Fecha oportuna de pago:</span>
    <div class="option-wrapper">
      <mat-form-field class="option">
        <mat-label>Día</mat-label>
        <input matInput formControlName="billDate" type="number" min="1" max="31">
      </mat-form-field>
      <p class="option-text">de cada mes.</p>
    </div>
    <span>Fecha límite de pago:</span>
    <div class="option-wrapper">
      <mat-form-field class="option">
        <mat-label>Día</mat-label>
        <input matInput formControlName="dueDate" type="number" min="1" max="31">
      </mat-form-field>
      <p class="option-text">de cada mes.</p>
    </div>
  </div>
</form>

dueDate字段的作用类似于一个超级字符,但billDate不起作用,返回:Error: Cannot find control with name: 'billDate',它正好是dueDate字段的副本。customerInterface和getCustomer查询都返回billDate和dueDate,但只有第二个有效。

von4xj4u

von4xj4u1#

你能不能把整个组件的代码给我看看?
另外,你能试试console.log(this.updateInvoiceDates)吗,你会得到什么?
您可以尝试将novalidate添加到模板中,如下所示:

<form class="form" [formGroup]="updateInvoiceDates" novalidate>
  //form content goes here
</form>

novalidate禁用HTML5表单验证,这有时会导致问题。

相关问题