AngularJs Global Constants Set and Get Variables

AngularJs Global Constants Set and Get Variables

In this Post We Will Explain About is AngularJs Global Constants Set and Get Variables With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to set global variable of Session using AngularJS Example

In this post we will show you Best way to implement AngularJS Global Variables Constants and Values With Example, hear for Set global constants and variables in AngularJs with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

READ :  Vue js Add active class to current Navigation Menu

Using the global root scope method AngularJs

Set global constants or variables app.js

[php]
// Set global constants or variables app.js
angular.module(‘liveApp’, [
‘ngCookies’,
‘ngResource’,
‘ngSanitize’,
‘ngRoute’
])
.config(function ($routeProvider) {
$routeProvider
.when(‘/’, {
templateUrl: ‘partials/main.html’,
controller: ‘liveCtrl’
})
.otherwise({
redirectTo: ‘/’
});
})
.run(function ($rootScope) {
$rootScope.mydataString = 41;
});
[/php]
Create a Main.js file using AngularJs

GET globalvariables in AngularJs main.js

[php]
// GET globalvariables in AngularJs main.js
angular.module(‘liveApp’)
.controller(‘liveCtrl’, [
‘$scope’, ‘$rootScope’,
function ($scope, $rootScope) {
console.log($rootScope.mydataString);
}
]);
[/php]

Using the global constant method AngularJs

Set global constants variables app.js

[php]
// Set global constants variables app.js
angular.module(‘liveApp’, [
‘ngCookies’,
‘ngResource’,
‘ngSanitize’,
‘ngRoute’
])
.constant(‘config’, {
mydataString: 41
})
.config(function ($routeProvider) {
$routeProvider
.when(‘/’, {
templateUrl: ‘partials/main.html’,
controller: ‘liveCtrl’
})
.otherwise({
redirectTo: ‘/’
});
});
[/php]

READ :  AngularJS Nested ng-repeat with Grouping Lists

Create a Main.js file using AngularJs

GET global variables in AngularJs main.js

[php]
// GET global variables in AngularJs main.js
angular.module(‘liveApp’)
.controller(‘liveCtrl’, [
‘$scope’, ‘config’,
function ($scope, config) {
console.log(config.mydataString);
}
]);
[/php]

Example

I hope you have Got What is Set global constants in AngularJs And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

Leave a Comment