How can i fix Error: formControlName must be used with a parent formGroup directive. - Angular ReactiveForms FormBuilder
How can i fix Error: formControlName must be used with a parent formGroup directive. - Angular ReactiveForms FormBuilder
我收到这个错误:
ERROR Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass it an existing FormGroup instance (you can create one in your class).
我已经在 module.ts
中导入了 import { ReactiveFormsModule } from '@angular/forms';
这是模板。html:
<mat-step [stepControl]="bankFormGroup" label="Some label">
<form [formGroup]="bankFormGroup">
<mat-form-field>
<mat-label>Banco</mat-label>
<mat-select ngDefaultControl formControlName="bank">
<mat-option *ngFor="let bank of banks" value="{{bank.id}}">
{{bank.id}} - {{bank.name}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-label>Agência</mat-label>
<input matInput formControlName="agency" placeholder="0001">
</mat-form-field>
<mat-form-field>
<mat-label>Número da Conta Corrente</mat-label>
<input matInput formControlName="account" placeholder="26262661">
</mat-form-field>
{...action form}
</form>
</mat-step>
这是 component.ts:
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
export class UploadComponent {
bankFormGroup: FormGroup = this._formBuilder.group({
bank: ['', Validators.required],
agency: ['', Validators.required],
account: ['', Validators.required]
});
banks: any[] = [
{ id: '001', name: 'Banco do Brasil S.A.' },
{ id: '237', name: 'Banco Bradesco S.A.' },
];
constructor(private _formBuilder: FormBuilder) { }
}
我已经在其他页面中看到其他类似的问题,例如
我正在使用:
"@angular/forms": "^11.0.0",
"@angular/core": "^11.0.0",
"@angular/material": "^11.0.3",
错误是在同一组件中以另一种形式出现。另一种形式没有 <form [formGroup]="FormGroupName">
parent..
检查是否有另一个没有 parent 容器且带有 [formGroup]
的表单
我收到这个错误:
ERROR Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass it an existing FormGroup instance (you can create one in your class).
我已经在 module.ts
中导入了import { ReactiveFormsModule } from '@angular/forms';
这是模板。html:
<mat-step [stepControl]="bankFormGroup" label="Some label">
<form [formGroup]="bankFormGroup">
<mat-form-field>
<mat-label>Banco</mat-label>
<mat-select ngDefaultControl formControlName="bank">
<mat-option *ngFor="let bank of banks" value="{{bank.id}}">
{{bank.id}} - {{bank.name}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-label>Agência</mat-label>
<input matInput formControlName="agency" placeholder="0001">
</mat-form-field>
<mat-form-field>
<mat-label>Número da Conta Corrente</mat-label>
<input matInput formControlName="account" placeholder="26262661">
</mat-form-field>
{...action form}
</form>
</mat-step>
这是 component.ts:
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
export class UploadComponent {
bankFormGroup: FormGroup = this._formBuilder.group({
bank: ['', Validators.required],
agency: ['', Validators.required],
account: ['', Validators.required]
});
banks: any[] = [
{ id: '001', name: 'Banco do Brasil S.A.' },
{ id: '237', name: 'Banco Bradesco S.A.' },
];
constructor(private _formBuilder: FormBuilder) { }
}
我已经在其他页面中看到其他类似的问题,例如 我正在使用:"@angular/forms": "^11.0.0",
"@angular/core": "^11.0.0",
"@angular/material": "^11.0.3",
错误是在同一组件中以另一种形式出现。另一种形式没有 <form [formGroup]="FormGroupName">
parent..
检查是否有另一个没有 parent 容器且带有 [formGroup]
的表单