Code Monkey home page Code Monkey logo

angular-using-ngmessages-readme's Introduction

Using ngMessages / ngMessage

Overview

When our inputs have lots of validation rules, writing out conditional ng-if statements for every type of validation can be very verbose. This is where a new module comes in - ngMessages!

Objectives

  • Describe ngMessages/ngMessage
  • Use ngMessages to display different types of validation errors

ngMessages

ngMessages is a separate module for Angular, so we need to make sure we require the file and module in our main module.

angular
	.module('app', [
		'ngMessages'
	]);

And then we're ready to use it!

What does it do?

ngMessages allows us to take an object and display different messages depending on what properties exist on that object.

This links in with our $error object that we had in the previous README. To refresh your memory, the $error object may look something like this for a field named username:

{
	username: {
		$error: {
			required: true
		}
	}
}

Let's pass the $error object into ngMessages. As a parent component, we use the ng-messages directive.

<form name="form">
	<input
	    name="username"
		ng-model="ctrl.username"
		required="required" />

	<div ng-messages="form.username.$error">
	</div>
</form>

Now that we've done that, we can use the directive ng-message for each property for which we want to display errors. As we've only got the possibility of required appearing in our object (as that is the only validation we've defined), we'll have one element:

<form name="form">
	<input
	    name="username"
		ng-model="ctrl.username"
		required="required" />

	<div ng-messages="form.username.$error">
		<div ng-message="required">Username is required!</div>
	</div>
</form>

We can use this on different inputs too. For instance, with an email, we get an email property. We can also add a minlength validation:

<form name="form">
	<input
	    name="email"
	    type="email"
		ng-model="ctrl.email"
		minlength="2"
		required="required" />

	<div ng-messages="form.email.$error">
	</div>
</form>

We'll now have an object that looks like this:

{
	email: {
		$error: {
			required: true,
			minlength: true,
			email: true
		}
	}
}

And now we can simply add more DOM elements for each error message:

<form name="form">
	<input
	    name="email"
	    type="email"
		ng-model="ctrl.email"
		minlength="2"
		required="required" />

	<div ng-messages="form.email.$error">
		<div ng-message="required">Email is required!</div>
		<div ng-message="minlength">Must be more than 2 characters!</div>
		<div ng-message="email">Must be a valid email!</div>
	</div>
</form>

It's worth noting that only one message will be displayed at a time, in order of the DOM nodes. At first, our required message will show, then if we type one letter in our input, the minlength message will show. Then when we've typed more letters, the email message will appear.

Only on $touched

We can also then add an ng-if to only show the error messages once the user has actually interacted with the input.

<form name="form">
	<input
	    name="email"
	    type="email"
		ng-model="ctrl.email"
		minlength="2"
		required="required" />

	<div ng-messages="form.email.$error" ng-if="form.email.$touched">
		<div ng-message="required">Email is required!</div>
		<div ng-message="minlength">Must be more than 2 characters!</div>
		<div ng-message="email">Must be a valid email!</div>
	</div>
</form>

Awesome! Let's compare this to what our previous code would've been:

<div ng-if="form.email.$touched">
    <div ng-if="form.email.$error.required">
        Email is required!
    </div>
    <div ng-if="form.email.$error.minlength">
        Email must be more than 2 characters!
    </div>
    <div ng-if="form.email.$error.email">
        Must be a valid email!
    </div>
</div>

You'll see we have cut down a lot of repeated code - also if we were to change the name of our input, we'd only have to change it once with ngMessages - 3 times (and maybe many more) without it.

View Using Ngmessages on Learn.co and start learning to code for free.

angular-using-ngmessages-readme's People

Contributors

annjohn avatar blazeiburgess avatar gj avatar ipc103 avatar toddmotto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-using-ngmessages-readme's Issues

missing topic

I think it would be good to have a note in here about adding the js/angular-messages.js file to index.html. In the next lesson it says "we've already added the file", but there's no mention about even having to do that in the first place.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.