gpt4 book ai didi

angular - 错误 : formControlName must be used with a parent formGroup directive. 您需要添加一个 formGroup 指令 - Angular react 形式

转载 作者:太空狗 更新时间:2023-10-29 16:54:23 34 4
gpt4 key购买 nike

我有以下模板。我正在尝试掌握响应式表单,但遇到了问题。

<form [formGroup]="guestForm" novalidate>
<div class="panel-body">
<form>
<div class="col-md-12 col-sm-12">
<div class="form-group col-md-3 col-sm-6">
<label>First Name* </label>
<input formControlName="firstname" type="text" class="form-control input-sm">
</div>
</div>
</form>
</div>
</form>

然后在我的组件中我有:

@Component({
selector: 'guest-input',
templateUrl: './guest-input.component.html',
})
export class GuestInputComponent implements OnInit {
@Input()
guest: Guest;

guestForm: FormGroup;

constructor(private _fb: FormBuilder) { }

ngOnInit() {
this.guestForm = this._fb.group({
firstname: ['test', [Validators.required, Validators.minLength(3)]]
});
}
}

这一切对我来说都很好,但出于某种原因我得到:

Error: Uncaught (in promise): Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroupdirective and pass it an existing FormGroup instance (you can create one in your class).

我以为我已经在我的 <form> 中声明了这一点.

最佳答案

你在带有 FormGroup 指令的 form 标签内嵌套了 form 标签,将其移除:

<form [formGroup]="guestForm" novalidate>
<div class="panel-body">
<form> -> Remove this
<div class="col-md-12 col-sm-12">
<div class="form-group col-md-3 col-sm-6">
<label>First Name* </label>
<input formControlName="firstname" type="text" class="form-control input-sm">
</div>
</div>
</form> -> Remove this
</div>
</form>

关于angular - 错误 : formControlName must be used with a parent formGroup directive. 您需要添加一个 formGroup 指令 - Angular react 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305975/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com