AngularJs全球服务
我正在尝试将响应变量存储在全局服务变量中。这是我的AngularJs全球服务,angularjs,Angularjs,我正在尝试将响应变量存储在全局服务变量中。这是我的服务: (function() { angular.module('employeeApp') .service('constants',constants); function constants() { this.url = 'http://domain.dev/api/v1/'; this.role = '', this.companyid
服务
:
(function() {
angular.module('employeeApp')
.service('constants',constants);
function constants() {
this.url = 'http://domain.dev/api/v1/';
this.role = '',
this.companyid = '',
this.name = ''
}
})();
登录工厂:
factory.login = function(email,password)
{
var vm = this;
vm.companyid = constants.companyid;
data = {"email": email, "password": password};
requestFactory.post(GLOBALS.url + 'login', data)
.then(function (response) {
vm.role = response.data.result.Employee.Role;
vm.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中的companyid时,它是空的。我做错了什么。正在寻找几个小时,但找不到解决方案 您需要将constants服务注入您的loginFactory。
此外,您没有在常量服务中返回对象嘿,您需要将您的服务作为依赖项注入工厂
angular.module('employeeApp')
.factory('loginFactory', ['constants', function(constants, $scope) {
$scope.login = function(email,password)
{
var vm = this;
vm.companyid = constants.companyid;
data = {"email": email, "password": password};
requestFactory.post(GLOBALS.url + 'login', data)
.then(function (response) {
vm.role = response.data.result.Employee.Role;
vm.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!');
});
}
}
除非你的应用程序的其他部分出现问题,否则这应该是可行的。请注意,函数
常量中使用分号而不是括号,模块声明中添加了方括号
(function() {
angular.module('employeeApp', [])
.service('constants',constants);
function constants() {
this.url = 'http://domain.dev/api/v1/';
this.role = '';
this.companyid = '';
this.name = '';
}
})();
请参见您的服务中没有退货声明。这可能是问题所在吗?@kubuntu我想在我的工厂里设置一些东西。将常量()
中的逗号改为分号。应该停止工作。请参阅下面答案中的工作演示。如果您的应用程序仍然存在问题,那么它们可能来自应用程序的其他部分。我已经这样做了:function authenticationFactory($cookieStore、$cookies、requestFactory、$location、GLOBALS、constants){
(function() {
angular.module('employeeApp', [])
.service('constants',constants);
function constants() {
this.url = 'http://domain.dev/api/v1/';
this.role = '';
this.companyid = '';
this.name = '';
}
})();