Angular Template Driven Forms
The Angular Template Driven Forms are simple forms which can be used to develop forms. These are called template driven as everything we are going to use in a application is defined into the template that we are defining along with the component.
-------------------------------------------------------------------------- Validations applied to controls with in a template is called TDF validations TDF validations is based on HTML5 validations, this will use html5 attributes 1.required 2.minlength 3.maxlength 4.pattern
Syntax:
<form #modelname="ngForm" [ngFormOptions]="{updateOn:'change|blur|submit'}">
<input [(ngModel)]="fieldname" name="controlname" required|minlength|maxlength|pattern="">
...
</form>
ngForm keyword will generate FormGroup object,this will be referenced
by modelname
->controlname will be considered as model property name,it is recommended
to provide controlname same as fieldname
->FormGroup and FormControl props[like invalid,touched,..] will be same as MDF validations
Steps
1 Import the namespace
NgForm:
It is the directive which helps to create the control groups inside form directive.
NgModel
When we add ngModel directive to the control, all the input elements are registered with the NgForm.
Next important thing is to consider is that when we use ngModel with form tag, then we should have to use the name property of the HTML control.
#studentForm is called the template reference variable and if you notice we have assigned “ngForm” as the value for the template reference variable studentForm. So the studentForm reference variable holds a reference to the form.
No comments:
Post a Comment
Thank you for visiting my blog