AngularJs设置了全局

时间:2016-03-25 10:03:58

标签: angularjs

我正在尝试为我的整个应用设置全局。但它不起作用。在这里,我宣布我的全局:

(function() {
    angular.module('employeeApp')
        .controller('authenticationController', authenticationController)
        .constant('GLOBALS', {
            url:'http://skindustries.dev/api/v1/',
            role:'',
            companyid:'',
            name:''
        });

然后,当员工登录时,我想设置全局变量。

function authenticationController(requestFactory,authenticationFactory,GLOBALS,$location,$cookieStore)
    {
    var vm = this;

        vm.login = function() {
            data = {"email": vm.email, "password": vm.password};
            requestFactory.post(GLOBALS.url + 'login', data)
                .then(function (response) {
                    console.log(response);
                    GLOBALS.role = response.data.result.Employee.Role;
                    GLOBALS.companyid = response.data.result.Employee.CompanyId;
                    authenticationFactory.setToken(response.data.result.Employee.api_token);
                    $cookieStore.put('employeeid', response.data.result.Employee.EmployeeId);
                    $location.path('/home');

                }, function () {
                    console.log('Niet ingelogd!');
                });
            }
        }

如果console.log(GLOBALS.role)结果中的authenticationControllersuperadministrator。然后用户被重定向到家。如果我console.log(GLOBALS.role)中的homeController

(function()
{
    angular.module('employeeApp').controller('homeController', homeController);

    function homeController(employeeFactory,GLOBALS) {
        console.log(GLOBALS.role);

结果是null

我在这里做错了什么!?

- 编辑 -

常数(服务)

(function() {
    angular.module('employeeApp')
        .service('constants',constants);

    function constants() {
        this.url = 'http://skindustries.dev/api/v1/';
            this.role =  'oldRole',
            this.companyid = '',
            this.name = ''
    }
})();

登录(工厂)

factory.login = function(email,password)
        {
            console.log('login');
            data = {"email": email, "password": password};
            requestFactory.post(GLOBALS.url + 'login', data)
                .then(function (response) {
                    constants.role = response.data.result.Employee.Role;
                    constants.companyid = response.data.result.Employee.CompanyId;
                    factory.setToken(response.data.result.Employee.api_token);
                    $cookieStore.put('employeeid', response.data.result.Employee.EmployeeId);
                    $location.path('/home');

                }, function () {
                    console.log('Niet ingelogd!');
                });
        }

的HomeController

(function()
{
    angular.module('employeeApp').controller('homeController', homeController);

    function homeController(constants) {
  console.log(constants.role);
}

2 个答案:

答案 0 :(得分:2)

基本上每次将值(或常量)注入控制器时都会初始化。所以它永远不会保留你的新价值,因此初始化它的旧价值。

根据您的需要,您可以在应用程序中使用service作为全局对象,以便在GLOBAL对象中保留新保存的值

Demo Fiddle

.service('GLOBALS', function() {
      this.url = 'http://skindustries.dev/api/v1/';
      this.role =  'oldRole',
      this.companyid = '',
      this.name = ''
  })

.controller('MyController', function(GLOBALS, $scope) {
  console.log(GLOBALS.role);
  $scope.role = GLOBALS.role;
  GLOBALS.role = "new role";
  console.log(GLOBALS.role);
})

.controller('MyController2', function(GLOBALS, $scope) {
  console.log(GLOBALS.role);
  $scope.role = GLOBALS.role;
});

为了更好地理解常量和值,请参阅此question

希望这有帮助。

答案 1 :(得分:-1)

一个常量不能被装饰器拦截,这意味着永远不应该改变一个常量的值。

使用价值:

angular.module('app', []);

.value('GLOBALS', 'The Matrix');

.controller('MyController', function (GLOBALS) {
  GLOBALS = "hello";
})