Introduction
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:
- var app = angular.module('ConstantApp', [])
then, create Config.js file and define Constant in it:
- app.constant('config', {
- appName: 'Constants',
- appVersion: 2.0
- });
Now, use the above declare Constant in our controller:
- app.controller('mainController', function ($scope,config) {
-
- $scope.ApplicationName = config.appName;
- }
At last now consume this scope in our HTML view:
- <title>{{ApplicationName}}</title>
Conclusion: You can use constants for a lot of things. this blog is just a basic demo explanation about constant.