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
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
ReplyDeleteBig 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
ReplyDeleteSpring 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
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
Kaliteli escort bayanlarla tanışma fırsatı yakalamak için tıkla: ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort - ankara escort
ReplyDeleteIt Is the best crack software website for all Mac and Windows users around the world.
ReplyDeleteARTICULATE STORYLINE
ReplyDeleteUninstalling CyberLink software permanently removes the application from your computer. Below you uninstall, you should: ... Not attempt to manually uninstall or remove CyberLink software by dragging folders to the Recycle Bin. Doing so can cause irreversible problems when you try to reinstall the product.
Click this LINK
wow this websites much more impress me.MKVToolNix Crack
ReplyDeleteThis impressed me so much amazing. Keep working and providing information
ReplyDeleteactivator
hi sir,i’d love to share this posting with the readers on my site. thanks for sharing this blogvmix crack
ReplyDeletehi Dear, Thank you for sharing your details and experience. I think it very good for me. Keep it up! Also May you like TopCrackWindows
ReplyDeleteMKVToolnix Crack is an amazing POST with good content.FINDCRACK is the best crack software site for all Mac and Windows users all over the world.
ReplyDeleteGood job and thanks for sharing such a good blog You’re doing a great job. Keep it up !!
ReplyDeleteWondershare Filmora Keygen
Avast Antivirus Crack is an internet security suite of cross-platform applications developed by Avast for Microsoft Windows, macOS, Android, and iOS.
ReplyDeletewindowsloader.org
Dear, I am for the first time, and I found beneficial information. Very nice and well-explained article. Thanks for sharing. Iris Pro Serial Key
ReplyDeletePC Crack
ReplyDeleteThank you for reading,
I hope this post was useful to you.
I appreciate you sharing such an informative and interesting post.
License Cracked
It Is the best crack software website for all Mac and Windows users around the world.
ReplyDeleteBandiZip serial key
I think F-Secure Freedome VPN is a fantastic Post with great content. CRACKDUE is the best cracked software site for all Mac and Windows users throughout the world.
ReplyDelete
ReplyDeleteGood Post. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.
Driver Easy Crack torrent
It’s great and fantastic piece. Keep it up as I look forward to read more from this website.
ReplyDeleteDVDFab Crack
I think Edraw Crack is a great Post with very good content. CRACKDUE is the best crack software site for all Mac and Windows users.
ReplyDeleteThis was really a fascinating matter, I am very lucky to have the ability to come to your blog and
ReplyDeleteI will bookmark this page so that I might come back one other time.
Lumion 11 Pro Crack
.Global Mapper Crack . is a great Post with useful information and great content. CRACK GURU is the most popular crack software website for Mac users and Windows users worldwide.
ReplyDeleteMinimum: 3.1 GB for MATLAB only, with no add-onsaiseesoft fonelab
ReplyDeletecrack
ReplyDeleteThanks for reading,
This post was hopefully helpful to you.
Your post was interesting and informative, so thanks for sharing.
Isobuster crack
NCH Pixillion Image Converter Plus Crack . is a great Post with useful information and great content. CRACK GURU is the most popular crack software website for Mac users and Windows users worldwide.
ReplyDeleteGood job and thanks for sharing such a good blog You’re doing a great job. Keep it up !!
ReplyDeleteMirillis Action activation key
It is the most popular crack software website for Mac users and Windows users worldwide.
ReplyDeleteUCRACKS
thank you giving us a great info about this blog. keep it up for good work. cracksco
ReplyDelete
ReplyDeleteit has a simple and easy to use interface. I've been using it for a long time and it is the best one indeed. Thanks for post this blog,is magnificent software for greater results of your task.
Musify Layest version
Good job and thanks for sharing such a good blog You’re doing a great job. Keep it up !!autocad 2017 crack
ReplyDeleteHi, I have to say I am impressed. I rarely come across such an informative and interesting blog,
ReplyDeleteand let me tell you that you nailed it. Windows 11 can be activated with this code.
It’s great and fantastic piece. Keep it up as I look forward to read more from this website.
ReplyDeleteAdobe Photoshop
Potential causes of hair loss in teenagers include genetic factors, hormonal imbalances, and underlying medical conditions. In some cases, hair loss can be reversible with proper treatment.
ReplyDeleteauslogicsbostspeed crack
This is a widespread and quite normal situation.licensecracked
ReplyDeleteVECTOR MAGIC CRACK content and information on the Post are excellent. For Mac and Windows users worldwide, This Site WELL CRACK is the best crack software website.
ReplyDeletePASSFAB FOR RAR CRACK content and information on the Post are excellent. For Mac and Windows users worldwide,
ReplyDeleteNavicat Premium Crack 2022
ReplyDeleteAvast Cleanup Premium
ReplyDeletenice and helpful post
ReplyDeletehttps://freecrackkey.com/microsoft-office-2015-crack/
Your article is very impressive, when I was going through the article I found you mention something that I was searching for a very long time. So, thanks for sharing. Create a Lyft Account
ReplyDeletemicrosoft office 2016 crack
ReplyDeleteMicrosoft Office 2016 is type of MS Office which help the users to write, modify and present the document.
This comment has been removed by the author.
ReplyDeleteYou Are The Best at what you do.Articulate Storyline Crack
ReplyDeleteThanks for sharing and visiting this site such a amazing content Download Free Crack Softwares
ReplyDelete