When we started writing Angular 2 apps, we had come from an AngularJS background. So of course our first forms were template driven. All we had to change from AngularJS was ng-model to ngModel and put it in a banana box. As long as the form's validation remains simple, Template driven forms are probably the way to go primarily due to their simplicity. However as the complexity of the forms grows, especially the validation of the form, its readability and even feasibility go bad pretty quickly. The only other downside to template driven forms is that they can't be unit tested. In our company most of these sorts of things were tested at an end-to-end level using Gherkin anyway, so less of an issue for us. But again as the complexity grows you might need to start unit testing those edge cases.
So let's take a relatively straightforward concrete example where the validation requirements might force us into implementing them in Reactive or Model-Driven forms. One side note about the nomenclature, we try to use Model Driven in our company, because we have React projects as well, and things can get pretty confusing distinguishing between a React form and a Reactive form. The example we are going to look at is a change password form. The validation requirements are that the passwords are strong, and that they match.
First let's take a look at the Template Driven HTML (change-password.component.html)
Change Password
{{error}}
And here is the TypeScript (change-password.component.ts)
export class ChangePasswordComponent { formErrors: string[]; oldPassword: string; newPassword: string; confirmNewPassword: string; constructor() { } changePassword() { // submit to server if (this.newPassword!==this.confirmNewPassword){ this.formErrors=["Passwords don't match"]; } else { this.formErrors=[]; } } }
So far no validation. It *is* possible to write template validation using directives, but it is much simpler using Model-driven forms. To convert over the first thing we need to remember is to add the ReactiveFormsModule to your module (app.module.ts)
import { FormsModule, ReactiveFormsModule } from '@angular/forms';and
imports:[ BrowserModule, FormsModule, ReactiveFormsModule
Then at the top of your component (change-password.component.ts) you will need to add:
import { FormBuilder, FormGroup } from '@angular/forms';
In the fields section of your component replace
oldPassword: string; newPassword: string; confirmNewPassword: string;with
form: FormGroup;
Lastly, change the constructor to this:
constructor (protected formBuilder: FormBuilder) { this.form=formBuilder.group({ oldPassword:[''], newPassword:[''], confirmNewPassword:[''] }); }
So basically there is one field where there use to be three, but the constructor was expanded to initialize the form with those three values. Now let's change the HTML (change-password.component.html). First find the form element, and change
to
Lastly, change all banana in a boxed ngModels, e.g. [(ngModel)] to formControlName. Here is an example change:
to
After doing that for all 3 fields. Voila! it is converted.
In Part 2 I will talk about the validation.
PS: For some reason Blogger and/or SyntaxHighlighter hate Angular code, it might be easier to read this over on my BrainHz blog
This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic
ReplyDeleteHadoop training institutes in chennai | Big data Hadoop Certification in chennai | Hadoop Training in Velachery | Hadoop Training in OMR | Hadoop Training with Placements
Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai
DeleteSpring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework the authors explore the idea of using Java in Big Data platforms.
Spring Training in Chennai
The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
All are saying the same thing, But it's a truth only. The post you have written is full of nice info. Keep on sharing!!
ReplyDeleteAngularjs Training in Chennai
Angularjs course in Chennai
Web Designing Course in chennai
PHP Training in Chennai
Hadoop Training in Chennai
AngularJS Training in OMR
AngularJS Training in Tambaram
Awesome Blog!!! Good to Read... Thanks for sharing with us.
ReplyDeleteEmbedded course in Coimbatore
embedded training in coimbatore
embedded systems course in coimbatore
RPA training in bangalore
Selenium Training in Bangalore
Java Training in Madurai
Oracle Training in Coimbatore
PHP Training in Coimbatore
Good job and thanks for sharing such a good blog You’re doing a great job. Keep it up !!
ReplyDeletePython Training in Chennai | Python Training in Chennai, OMR | Python Training in Chennai, Velachery | Best Python Training in Chennai | Python Training Institute in Chennai | Best OpenStack Training in Credo Systemz, Chennai
Quickbooks Pricing and Plans
ReplyDelete
ReplyDeleteGet the most advanced Python Course by Professional expert. Just attend a FREE Demo session.
For further details call us @ 9884412301 | 9600112302
Python training in chennai | Python training in velachery
Amazing Article,Really useful information to all So, I hope you will share more information to be check and share here.
ReplyDeleteinternship in chennai for electrical engineering students
one month internship in chennai
vlsi internship in chennai
unpaid internship in chennai
internship for hr in chennai
internship training chennai
internship for freshers in chennai
internship in chennai for it students with stipend
internship in accenture chennai
naukri internship in chennai