FormGroupDirective is a directive in Angular Forms that is used to bind an HTML form element with a FormGroup instance in the component class. The FormGroup instance contains a set of form controls and their values, which can be used to validate and submit the form.
To use FormGroupDirective, you need to import it from @angular/forms and add it to the form element using the directive selector [formGroup]:
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { FormGroupDirective } from '@angular/forms';
@Component({
selector: 'app-example',
template: `
`,
})
export class ExampleComponent {
exampleForm: FormGroup;
constructor(private fb: FormBuilder) {
this.exampleForm = this.fb.group({
name: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
});
}
onSubmit() {
// handle form submission
}
}
In the above example, we define a FormGroup instance using FormBuilder and bind it to the form element using FormGroupDirective. The form contains two form controls - name and email - with validation rules added using Validators. When the form is submitted, the onSubmit() method is called where we can handle the form submission. 免责声明:本文内容通过AI工具匹配关键字智能整合而成,仅供参考,火山引擎不对内容的真实、准确或完整作任何形式的承诺。如有任何问题或意见,您可以通过联系service@volcengine.com进行反馈,火山引擎收到您的反馈后将及时答复和处理。