create and use constants in angularjs

Solutions on MaxInterview for create and use constants in angularjs by the best coders in the world

showing results for - "create and use constants in angularjs"
Alexander
21 Jul 2020
1// Storing a single constant value
2var app = angular.module('myApp', []);
3
4app.constant('appName', 'My App');
5
6// Now we inject our constant value into a test controller
7app.controller('TestCtrl', ['appName', function TestCtrl(appName) {
8    console.log(appName);
9}]);
10
11// Storing multiple constant values inside of an object
12// Note: values in the object mean they can be modified
13var app = angular.module('myApp', []);
14
15app.constant('config', {
16    appName: 'My App',
17    appVersion: 1.0,
18    apiUrl: 'http://www.facebook.com?api'
19});
20
21// Now we inject our constant value into a test controller
22app.controller('TestCtrl', ['config', function TestCtrl(config) {
23    console.log(config);
24    console.log('App Name', config.appName);
25    console.log('App Name', config.appVersion);
26}]);
27