Global variables in AngularJS

前端 未结 12 1934
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 12:05

I have a problem where i\'m initialising a variable on the scope in a controller. Then it gets changed in another controller when a user logs in. This variable is used to co

12条回答
  •  一个人的身影
    2020-11-22 12:34

    // app.js or break it up into seperate files
    // whatever structure is your flavor    
    angular.module('myApp', [])    
    
    .constant('CONFIG', {
        'APP_NAME' : 'My Awesome App',
        'APP_VERSION' : '0.0.0',
        'GOOGLE_ANALYTICS_ID' : '',
        'BASE_URL' : '',
        'SYSTEM_LANGUAGE' : ''
    })
    
    .controller('GlobalVarController', ['$scope', 'CONFIG', function($scope, CONFIG) {
    
        // If you wish to show the CONFIG vars in the console:
        console.log(CONFIG);
    
        // And your CONFIG vars in .constant will be passed to the HTML doc with this:
        $scope.config = CONFIG;
    }]);
    

    In your HTML:

    {{config.APP_NAME}} | v{{config.APP_VERSION}}
    

提交回复
热议问题