What is @angular/forms?
The @angular/forms package is a part of the Angular framework that provides tools for creating and managing forms in Angular applications. It offers two approaches: Reactive Forms and Template-driven Forms, which allow developers to handle user input and form validation in a robust and scalable way.
What are @angular/forms's main functionalities?
Reactive Forms
Reactive Forms provide a model-driven approach to handling form inputs whose values change over time. This feature allows for more flexible, scalable, and testable forms.
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
// other imports ...
ReactiveFormsModule
],
})
export class AppModule { }
Template-driven Forms
Template-driven Forms are useful for adding simple forms to your app with minimal setup. They are less flexible than Reactive Forms but can be easier to use for basic scenarios.
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
// other imports ...
FormsModule
],
})
export class AppModule { }
Form Validation
The package provides built-in validators as well as the ability to create custom validators, making it easy to implement complex validation logic for form fields.
import { FormControl } from '@angular/forms';
let email = new FormControl('', [Validators.required, Validators.email]);
Form Groups
Form Groups are used to group together multiple form controls into one object, allowing for easier management of form data and validation.
import { FormGroup, FormControl } from '@angular/forms';
let userForm = new FormGroup({
firstName: new FormControl(),
lastName: new FormControl(),
age: new FormControl()
});
Other packages similar to @angular/forms
react-hook-form
React Hook Form is a library for React that enables the management of forms with minimal re-renders. It is similar to @angular/forms in providing form validation and form state management, but it is designed specifically for React and uses hooks.
formik
Formik is another popular form library for React. It simplifies the process of building, validating, and handling forms. It is comparable to @angular/forms in its functionality but is tailored to the React ecosystem.
redux-form
Redux Form leverages Redux for form state management in React applications. It is similar to @angular/forms in that it handles form state and validation, but it integrates tightly with Redux for state management.
15.0.1 (2022-11-22)
common
| Commit | Type | Description |
| -- | -- | -- |
| 930af9dd26 | fix | Fix MockPlatformLocation events and missing onPopState implementation (#48113) |
forms
| Commit | Type | Description |
| -- | -- | -- |
| b342e55509 | fix | don't mutate validators array (#47830) |
| a12a120272 | fix | FormBuilder.group return right type with shorthand parameters. (#48084) |
language-service
| Commit | Type | Description |
| -- | -- | -- |
| cc8b76ef7c | fix | correctly handle host directive inputs/outputs (#48147) |
| a8c33bf931 | fix | update packages/language-service/build.sh script to work with vscode-ng-language-service's new Bazel build (#48120) |
router
| Commit | Type | Description |
| -- | -- | -- |
| e4309d57d8 | fix | correct type of nextState parameter in canDeactivate (#48038) |
| 9baefd085f | fix | Ensure renavigating in component init works with enabledBlocking (#48063) |
| fa5528fb5f | fix | restore 'history.state' on popstate even if navigationId missing (#48033) |
Special Thanks
Alan Agius, Andrew Scott, Bjarki, Bob Watson, Brooke, Derek Cormier, Dylan Hunn, George Kalpakas, Greg Magolan, Ikko Ashimine, Ivan Rodriguez, Jessica Janiuk, Joe Roxbury, Joey Perrott, Kristiyan Kostadinov, Matthieu Riegler, Mikhail Savchuk, Nebojsa Cvetkovic, Pawel Kozlowski, Volodymyr and Wooshaah
<!-- CHANGELOG SPLIT MARKER -->
<a name="12.2.17"></a>