Book Image

AngularJS Web application development Cookbook

By : Matthew Frisbie
Book Image

AngularJS Web application development Cookbook

By: Matthew Frisbie

Overview of this book

Packed with easy-to-follow recipes, this practical guide will show you how to unleash the full might of the AngularJS framework. Skip straight to practical solutions and quick, functional answers to your problems without hand-holding or slogging through the basics. Avoid antipatterns and pitfalls, and squeeze the maximum amount out of the most powerful parts of the framework, from creating promise-driven applications to building an extensible event bus. Throughout, take advantage of a clear problem-solving approach that offers code samples and explanations of components you should be using in your production applications.
Table of Contents (17 chapters)
AngularJS Web Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating and integrating custom form validators


With the addition of the validator pipeline, AngularJS's form validation is now highly extensible and straightforward to expand.

How to do it…

Formerly, custom form validation required messiness involving parsers and formatters; this is no longer the case. Custom validation can now be encapsulated cleanly within a directive.

Synchronous validation

The ngModel directive now exposes the $validators property, which allows you to directly tap into its form validation.

The following directive definition is an example of a custom validator that ensures that a model value is not Packers:

(app.js)

angular.module('myApp', [])
.directive('validateFavoriteTeam', function() {
  return {
    require : 'ngModel',
    link : function(scope, element, attrs, ngModel) {
      // define custom validator "favoriteTeam"
      ngModel.$validators.favoriteTeam = function(team) {
        // check string inequivalency 
        // a false return value indicates an error...