UrbanPro
true

Learn Angular.JS from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Angular 4 Forms: Template Driven Approach

Suprasanna Bhaumik
05/09/2017 0 0

Angular Forms:

Forms in Angular 4 can be designed on two approaches.

As you can see below , we have a Template based design and the other being a Reactive based design.

Here we will try to understand the Template based design:

In the template driven approach all the functionalities are added to the Form(HTML).

To get started with the template driven approach, we need to import the FormsModule from '@angular/forms' and import them into our application module file.

Now let us consider the below HTML code: 

  • As soon as Angular sees the form tag it understands that it has to create the Javascript object representing our form. So to make it understand that this is indeed an Angular form we have marked the Local reference (f)  as ngForm.  ngForm is a directive provided by Angular and has numerous functions related to forms.
  • We have two input fields "username" and "email". To make the Angular form know that these fields are required in the javascript form object , we have to add ngModel. ngModel marks these parameters for the Angular form. 
  • The Javascript Form object maintains a key value pair of the form controls. So we need to provide the key (the value is marked by the ngModel) which is the name attribute in the form elements. E.g. Checkout the name attribute in our input tags.
  • We can also see that we don't have an action attribute mentioned in our form. This is because, we do not want to hit any service before validating our form.
  • Submit button click does not have any function call on it. However we can see that there is a (ngSubmit) directive, that takes advantage of the default Javascript behaviour and has a callback function to handle our event.

With the above changes made into our HTML we are ready to access the Javascript Form object in our typescript file.

The Typescript file:

  • We can access the Form element by passing the local reference( in our case f )                     
  • To access the Javascript Form object we need to use ngForm                                             
  • One other alternative is to use the ViewChild property that also gives us the same Javascript form object.                                                                                                   

A typical Javascript form object is shown below: 

For more details into the other aspects of Form handling such as Validators, setting default values, using the form object, Reactive Forms and many more, register for my Angular 4 course.

Thanks and Happy Reading!

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Angular-2 Developer Training Program Syllabus
Angular2 Developer Training Program Syllabus: Introduction to Angular2. Why Angular2. Angular2 Features: Components, Services and Typescript. Angular2 Components: Templates, Modules, Services...

Why AngularJS Got Popular
AngularJS became very popular due to its easy two-way data binding, MVC architecture, built-in module system, dependency injection, and routing package. Initially built as a hobby project by Misko Hevery...

AngularJS Program Syllabus
What You will Learn in AngularJS Program? Introduction to AngularJS. AngularJS Expressions: Numbers, Strings, Objects, Arrays, Expressions using {{ }} and ng-bind. Modules: Creating a module, adding...

Angular Js Workshop & Courses
AngularJS is an open source JavaScript framework by Google to build web applications. It can be freely used, changed and shared by anyone. Prerequisite: Before learning AngularJS, you must...

Why Angular JS?
Interactive Web Application HTML and CSS are not interactive. You cannot create interactive applications using HTML and CSS. JavaScript allows you to interact with a web page like clicking a button,...
P
X

Looking for Angular.JS Classes?

The best tutors for Angular.JS Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Angular.JS with the Best Tutors

The best Tutors for Angular.JS Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more