Understand, Template-driven forms
Learn how template-driven forms and model-driven forms vary and resemble one another.
How to connect template input controls to component properties using the ngModel directive.
How to use the template-driven technique to implement form validation.
In the template-driven technique, how to submit and reset a form.
Overview
The simplest method for building forms in an Angular application is to use template driven forms. There is no requirement that we establish FormGroup and FormControl inside the Component.
Template-driven forms are just model-driven forms but driven by directives. Binding the HTML form to the component's model object is made possible via built-in directives in Angular. FormGroup and FormControl
First, using a few form elements, we construct a straightforward HTML form. The top-level FormGroup control will be created and converted to a template-driven form via the ngForm directive. Then, for each of the HTML form elements, we build a FormControl instance using the ngModel directive.
Setup An Initial Form Template
First, using a few form elements, we construct a straightforward HTML form. The top-level FormGroup control will be created and converted to a template-driven form via the ngForm directive. Then, for each of the HTML form elements, we build a FormControl instance using the ngModel directive.
1. Import FormsModule
We must import the FormsModule in order to use template-driven forms. The root module or a shared module is where we typically import it. All form directives and structures required to work with forms are included in the FormsModule.
2. Setup An Initial Form Template inside our Template (A Normal form Not a Template Form)
3. Lets Convert our above simple form into a Template Driven Form
Directive : NgForm And NgModel
The Angular directives ngForm and ngModel are necessary for building Angular Template Driven Forms.
ngForm Creates a top-level FormGroup instance and binds it to a form to track aggregate form value and validation status.
NgForm directive has a selector which matches the HTML form tag.
local template reference variable
If we execute this code right now, a blank object will be produced. This is due to the ngForm directive's inability to recognise every control present within the form> element it is connected to. With the ngForm directive, each template control needs to be manually registered. For this we need to do two things with each template form control:
Add the ngModel directive
Add the name attribute
To manage the template form control, the NgModel directive produces an instance of FormControl, and the name attribute instructs NgModel which key to keep for that FormControl in the parent FormGroup.
Two-way data binding in Template Form
Using two-way data binding is another method for interacting with template-driven forms.
We may use ngModel directive to build two-way data binding between a template form control and a variable on our component.
Therefore, whenever the user modifies the value in the template form control, the component's variable value immediately updates, and vice versa when we modify the component's variable.
Create a variable inside the component to which we will map the control before using the following syntax in the template control tag to map a template control using two-way data binding.
one way binding- to give that control a default value
two way binding- to instantly output control value
Submit Form
To get access to form. add some local reference inside form tag #f here and pass same as an argument in onSubmit method
add directive ngForm inside it - it tells angular - please give access to JS object of this form you created automatically.
Fill the form and submit. now we can actual JS object created by angular.
Here we can see that each control is of type FormControl and having multiple properties.
dirty property for example- it remain false until we change value of that control.
touched. - whether we focused on that field?
However, we may use a ViewChild decorator in our component code to obtain a reference to the ngForm object.
With the help of this decorator, we can refer to something from our template in our component.
Finally, we use the ViewChild decorator to decorate our property. The name of the local reference variable we want to connect to is passed to ViewChild as follows:
Summary
The Template-driven forms is set up using ngForm directive
controls are set up using the ngModel directive
ngModel also provides the two-way data binding
The Validations are configured in the template via directives