Ich versuche, eine globale für meine gesamte App festzulegen. Aber es funktioniert nicht. Hier deklariere ich meine Globals:AngularJs setzen eine globale
(function() {
angular.module('employeeApp')
.controller('authenticationController', authenticationController)
.constant('GLOBALS', {
url:'http://skindustries.dev/api/v1/',
role:'',
companyid:'',
name:''
});
Dann, wenn ein Mitarbeiter mich anmeldet, möchte ich Globals setzen.
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!');
});
}
}
Wenn ich console.log(GLOBALS.role)
in authenticationController
Ergebnis superadministrator
ist. Dann wird der Benutzer nach Hause umgeleitet. Wenn ich console.log(GLOBALS.role)
in meinem homeController
.
(function()
{
angular.module('employeeApp').controller('homeController', homeController);
function homeController(employeeFactory,GLOBALS) {
console.log(GLOBALS.role);
Ergebnis ist null
?
Was mache ich hier falsch?
--EDIT--
Konstante (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 (Fabrik)
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
Haben Sie überprüft, ob 'GLOBALS' als Winkelkonstante registriert wird, bevor" homeController "ausgeführt wird? – nalinc
Ja, ich habe das überprüft! – Jamie
Ich empfehle Ihnen, einen Blick auf diesen Blogbeitrag zu werfen, der sich mit Auth befasst: http://www.jvandemo.com/learn-how-to-make-authentication-in-your-angular-applications-simpler-and-more -consistent/ –