In this 22nd day of AngularJS article series, we are going to be learning next key players/concept of AngularJS, understanding the concept of ngMessage directive of AngularJS. Before moving to key players/concepts of AngularJS, please read my previous articles:
- Introduction to AngularJS – Day 1
- Introduction to AngularJS – Day 2
- Introduction to AngularJS – Day 3
- Introduction to AngularJS – Day 4
- Introduction to AngularJS – Day 5
- Introduction to AngularJS – Day 6
- Introduction to AngularJS – Day 7
- Introduction to AngularJS – Day 8
- Introduction to AngularJS – Day 9
- Introduction to AngularJS – Day 10
- Introduction to AngularJS – Day 11
- Introduction to AngularJS – Day 12
- Introduction to AngularJS – Day 13
- Introduction To AngularJS – Day 14
- Introduction To AngularJS – Day 15
- Introduction To AngularJS – Day 16
- Introduction to AngularJS – Day 17
- Introduction to AngularJS – Day 18
- Introduction to AngularJS – Day 19
- Introduction To AngularJS - Day 20
- Introduction To AngularJS – Day 21
In AngularJS – 1.3 defines new directive for us that is ‘ngMessage’. This directive is used for displaying messages or error messages to user. This messages are displayed order as they are listed. This directive is specially designed to show and hide messages based on their state.
A remote template or custom error message is also used for message reusability and messages can be overridden. It can be used in the following ways:
- As an attribute directive.
<!-- attribute-style usage -->
<div ng-messages="empForm.email.$error">...</div> - As an element directive.
<!-- element-style usage -->
<ng-messages for="empForm.email.$error">...</ng-messages>
Following are the list of directives added by AngularJS as follows, we are going to learn one by one:
ng-messages
This directive is designed to show and hide messages based on their states. It uses the ‘$error’ object for the states.
ng-message
This directive is used to show and hide particular messages. It determines which messages are visible or shown base on the state provided by ngMessages.
ng-message-include
This directive defines itself, means include other remote message on proper place. It is place in ngMessages directive container. Using this directive you can reuse the previously defined messages and you can override also that messages.
ng-message-exp
This directive works the same as ngMessage directive. You can show and hide messages with the help of passing expression to this message.
Steps to use this directive in your applications:
- Download or used CDN path of ‘angular.messages.js’ file in your application as follows:
- <script src="app/angular.min.js"></script>
- <script src="app/angular-messages.js"></script>
- Inject the ‘ngMessages’ module in your main module as follows:
- var app = angular.module('mainApp', ['ngMessages']);
Example:
In the following example we are going to see how we can use this directive to showing form validation messages as follows:
app.js
- /*Angular Modules take a name, best practice is lowerCamelCase, and a list of dependancies*/
- var app = angular.module('mainApp', ['ngMessages']);
- //Creating controller in general
- app.controller('SimpleCtrl', function ($scope) {
- $scope.name = 'Jeetendra Gund';
- });












Neeraj KumarPosted May 6, 2016, 1:43 PM
Good work
Sr KarthigaPosted May 6, 2016, 3:40 AM
good one
NitinPosted May 6, 2016, 3:26 AM
good one
Anbu ManiPosted May 6, 2016, 2:36 AM
Nice
Kuppurasu NagarajPosted May 5, 2016, 1:02 PM
Nice Sharing..
Debasis SahaPosted May 5, 2016, 9:41 AM
Good one..
Raja TPosted May 5, 2016, 9:19 AM
Nice, Thanks for sharing
Vignesh ManiPosted May 5, 2016, 8:23 AM
Good work
Thiruppathi RPosted May 5, 2016, 5:04 AM
Nice One...