Angular forms are a way to handle user input in Angular applications. They provide a way to collect, validate, and process user data.
There are two main types of forms in Angular:
To create a template-driven form, follow these steps:
<form #myForm="ngForm">
<input type="text" name="username" ngModel required>
<button type="submit">Submit</button>
</form>
In your component, you can access the form data using the `myForm` variable.
To create a reactive form, you need to import `ReactiveFormsModule` in your application module:
import { ReactiveFormsModule } from '@angular/forms';
Then, create a form in your component:
import { FormGroup, FormBuilder } from '@angular/forms';
export class MyComponent {
myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
username: ['', Validators.required]
});
}
}