Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 833 of the free egghead.io lessons, plus get Angular 1.x content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Introduction to ng-messages for AngularJS

4:13 Angular 1.x lesson by

ng-messages is a directive added in AngularJS 1.3 that is used to display messages to the user. This is usually in relation to form error messages, and cleans up the syntax required to show feedback messages to the user.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

ng-messages is a directive added in AngularJS 1.3 that is used to display messages to the user. This is usually in relation to form error messages, and cleans up the syntax required to show feedback messages to the user.

Avatar
Paul

I see ng-model is a requirement on the input to get messages working. Not quite sure how it works though. It seems arbitrary as to what the model is, e.g. ng-model="foo" works equally as well as ng-model="inputname". Could you explain the binding here?

In reply to egghead.io
Avatar
Kent C.

Hi Paul,
Here are the docs for ng-model. It's a fundamental directive for model-to-form binding in Angular: https://docs.angularjs.org/api/ng/directive/ngModel

In reply to Paul

This is another video about the cool new things in Angular 1.3. This is the first video I've recorded where Angular 1.3 is actually officially released. You can see I'm bringing in 1.3 here. We're going to be talking about ng-messages, which is a core module, provided by the Angular team, that helps with form validation.

You can see here I'm including that script as well, the Angular messages script from the Angular Google CDN. All that you really need to depend on this module is have it listed in your dependencies for when you create your own module. We are creating a module called "app," and we depend on ng-messages.

Then we have our just very simple controller. We're using controller as syntax and printing out our version here, and then we're going to put in our demo stuff here.

From here, we're not going to be doing any more Javascript stuff. This is all going to be in our template, so we'll hide that. We're just going to make a form with the name "myForm."

Let's first make this form as if we didn't have the ng-messages module. Let's just say you have an input. The type is email. We'll bind it to a field. It'll be required. We'll say, "minlength of five."

Generally, what you do these days is maybe you'd have a div or something, and you'd say, "ng-show myForm." Oh, and this needs a name, so "myField." Let's drag this over a little bit.

We'll say, "myForm.myField.$error." We're only going to show this div when there is an error for that field, and then we'll have another div for the specific error message, so "ng-show."

We're going to want to take that whole field there. When there's specifically a required error, this field is required. We're getting that error right there.

Then we'll also have a couple more errors that you can have. You can make it not an email, or you can make it minlength and email. Let's say, "Input too short" and "This field must be an email."

Let's just try it out. Hello. Now we get our errors there. Now one error. Now it's gone. This is the way that you would do things traditionally, and there are a couple of problems with this.

One is you're repeating yourself like crazy in your template, and this isn't very fun. Another thing is you're bombarding the user with a bunch of errors that they have to think about all at once, and some would argue it's a better user experience to just show them one error at a time, have them fix one thing at a time until they have solved the problem.

If you can imagine, just look at this and think, "How would I make sure that there's only one error showing at a time?" Knock yourself out.

Ng-messages brings some really cool stuff to the table. We're going to change this ng-show with ng-messages. We'll make this an ng-message, and this one is a message and this one.

Now we don't need this anymore because we're passing that to our ng-messages directive, and each one of these directives that are children of that ng-messages directive will be basically...

You could almost think of it as the expressions being appended onto the ng-messages expression. It's going to check the object that we get from this error here for these messages to be shown. Here, now if we say, "Hello," now you see only one error is being shown at a time, and our template is a lot simpler.

It is possible for you to get the multiple error messages at a time. That's not a problem. All you need to do is on this element here, you say, "ng-messages-multiple," and then you get multiple error messages.

If you want to have that, you have that option available to you. There are a couple other really cool things that I'll be showing in future videos, about ng-messages, but here's your intro to ng-messages.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?