I'm trying to set a global for my entire app. But it's not working. Here I declare my globals:
(function() {
angular.module('employeeApp')
.controller('authenticationController', authenticationController)
.constant('GLOBALS', {
url:'http://skindustries.dev/api/v1/',
role:'',
companyid:'',
name:''
});
Then when a employee signs in I want to set globals.
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!');
});
}
}
If I console.log(GLOBALS.role) in authenticationController result is superadministrator. Then the user is redirected to home. If I console.log(GLOBALS.role) in my homeController.
(function()
{
angular.module('employeeApp').controller('homeController', homeController);
function homeController(employeeFactory,GLOBALS) {
console.log(GLOBALS.role);
Result is null?
What am I doing wrong here!?
--EDIT--
constant (service)
(function() {
angular.module('employeeApp')
.service('constants',constants);
function constants() {
this.url = 'http://skindustries.dev/api/v1/';
this.role = 'oldRole',
this.companyid = '',
this.name = ''
}
})();
login (factory)
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);
}
GLOBALSis being registered as angular constant "before" 'homeController' gets executed ?