We can just say
Reactive form can be used in the following situations
- Complex forms with a large number of fields.
- Multiple complex validation is. User Checks Required
- Require that a JSON structure be sent with values ββin the form.
We can get the whole form in a structured way using "form.value"
If we have 4 fields Name, Surname, Email, Phone number in reactive form.
HTML code will be
<form [formGroup]="form"> First Name <input formControlName="firstName"> Last Name <input formControlName="lastName"> Email <input formControlName="email"> Phone Number <input formControlName="phoneNumber"> </form>
We can get the values ββfrom the form as shown below
{ "firstName": "FName", "lastName": "LName", "email": " test@123.com ", "phoneNumber": "123" }
calling form.value, where form is the FormGroup variable we created.
Template-based form : Can be used with simple forms. Like the login page. With two way data binding. We can simply assign a value to a variable from the user interface and vice versa.
A simple example: if we provide two-way binding for input below.
<input [(ngModel)]="username">
We can simply display the value that the user gives in the user interface.
<p>Hello {{username}}!</p>
vivekkurien
source share