Beginning AngularJS

(WallPaper) #1

CHApTer 6 ■ WorkIng WITH ForMS


This revision means that our input[name='email'].ng-dirty.ng-valid rule kicks in. Consequently, the text
becomes green. This is quite a powerful technique, because once you know which classes AngularJS is adding
and when, you can use these classes as hooks for just about anything you like. Of course, just because you can
doesn’t mean that you should! There was a little bit of legwork involved in building the validation and validation
feedback code into our registration form, but it wasn’t overly complicated. This has been a relatively long chapter,
and I recommend that you load up this code in your web browser and favorite IDE; it is well worth doing some
experimentation with it. The finished version can be found in Listing 6-19 and Listing 6-20. Admittedly, the validation
rules that we studied here were fairly basic, but the general approach we used can take you quite far.


Listing 6-19. Registration Form—the HTML Code



Form input has not yet started

Form input has started




Please enter a value for First name





Please enter a value for Last name





A valid email address is required



<select name="research" ng-model="person.levels"
ng-options="obj.label as obj.value for obj in person.channels" required>





Please tell us where you heard about us



<input ng-model="person.newsletterOptIn" type="checkbox" name="newsletterOptIn"
id="newsletterOptIn" value="newsletterOptIn"/>








Thank you for taking the time to register!

Free download pdf