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 :  Angular ng model Directive Example

Using the global root scope method AngularJs

Set global constants or variables app.js

// 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;
    });

Create a Main.js file using AngularJs

GET globalvariables in AngularJs main.js

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

Using the global constant method AngularJs

Set global constants variables app.js

// 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: '/'
            });
    });

Create a Main.js file using AngularJs

READ :  Vuejs Dynamic Searching and Sorting Table pagination

GET global variables in AngularJs main.js

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

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.

Add a Comment

Your email address will not be published. Required fields are marked *