Constant in AngularJS

This blog will let you know about Constant in AngularJS.

Constant are like services in AngularJS in which we can define our globally data. It is declare using "constant" keyword.
As we define our app-keys in Web.Config file for ASP.NET application, which further we can use anywhere in the application, like wise same we can declare constant data in AngularJS globally that can be used throughout the application.
We can inject Constant everywhere in controller or service like any other dependency (e.g.$http). AngularJS used Singleton structure for creating Constant dependency in our Angular application.
So, using the Constant you can create your Config.js file and can be inject anywhere in your application.
Now, let's start to define constant and will use in controller.
First of all create angular module:
  1. var app = angular.module('ConstantApp', [])  
then, create Config.js file and define Constant in it:
  1. app.constant('config', {  
  2. appName: 'Constants',  
  3. appVersion: 2.0  
  4. });  
Now, use the above declare Constant in our controller:
  1. app.controller('mainController'function ($scope,config) {  
  3. $scope.ApplicationName = config.appName;  
  4. }  
At last now consume this scope in our HTML view:
  1. <title>{{ApplicationName}}</title>  
Conclusion: You can use constants for a lot of things. this blog is just a basic demo explanation about constant.