2016-03-25 93 views
0

我想为我的整个应用程序设置一个全局。但它不起作用。这里我声明我的全局变量: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)authenticationController结果是superadministrator。然后用户被重定向回家。如果我在我的homeControllerconsole.log(GLOBALS.role)

(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); 
} 
+0

在''homeController'执行之前,您是否核对过'GLOBALS'正在被注册为角度常量? – nalinc

+0

是的,我检查过! – Jamie

+0

我建议你看看这篇关于auth的博客文章:http://www.jvandemo.com/learn-how-to-make-authentication-in-your-angular-applications-simpler-and-more -consistent/ –

回答

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

希望这会有所帮助。

+0

谢谢,但是当我尝试这个并重新加载页面时,角色消失了。请参阅我的编辑。 – Jamie

+0

显然@jamie编辑后的角色将不会被保存在会话或外部数据库中。如果您希望保存更改的GLOBAL值,则应使用localStorage(即特定于浏览器)或数据库来保存数据。 –

+0

将它存储到cookie中会不对? – Jamie

-1

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

使用价值:

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

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

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