Javascript 调用返回未定义的服务
我正在创建一个名为Javascript 调用返回未定义的服务,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我正在创建一个名为ActiveUserProfileService的服务,但当我在控制器中调用它的函数时,结果是未定义,我无法找出原因。最奇怪的是,在ActiveUserProfileService服务中,来自UserService的信息通过控制台.log显示,因此我收到了信息,但在控制器中调用ActiveUserProfileService后,它给了我未隔离的。似乎数据没有被传递。有人能帮我吗 UserService.js: (function () { 'use strict';
ActiveUserProfileService
的服务,但当我在控制器中调用它的函数时,结果是未定义
,我无法找出原因。最奇怪的是,在ActiveUserProfileService
服务中,来自UserService的信息通过控制台.log
显示,因此我收到了信息,但在控制器中调用ActiveUserProfileService
后,它给了我未隔离的。似乎数据没有被传递。有人能帮我吗
UserService.js:
(function () {
'use strict';
angular
.module('myApp')
.factory('UserService', UserService);
UserService.$inject = ['$http'];
/* @ngInject */
function UserService($http) {
var service = {
getAuthenticatedUser: getAuthenticatedUser,
getUserInformation: getUserInformation
};
return service;
function getUserInformation(idUser) {
return $http.post('api/user/details', {idUser: idUser});
}
function getAuthenticatedUser() {
return $http.get('api/user');
}
}
})();
ActiveUserProfileService.js
(function () {
'use strict';
angular
.module('myApp')
.factory('ActiveUserProfileService', ActiveUserProfileService);
ActiveUserProfileService.$inject = ['$http','UserService'];
/* @ngInject */
function ActiveUserProfileService($http, UserService) {
var service = {
isAccount: isAccount
};
return service;
////////////////
function isAccount(accountName) {
UserService.getAuthenticatedUser()
.then(function (response) {
var data = response.data;
UserService.getUserInformation(data.user.id)
.then(function (response) {
var userDetails = response.data;
console.log("It is");
console.log(accountName == userDetails.account_types[0].description_internal);
return accountName == userDetails.account_types[0].description_internal;
});
})
}
}
})();
我的控制器:
(function () {
'use strict';
angular
.module('myApp')
.controller('WizardController', WizardController);
WizardController.$inject = [
'UserService',
'ActiveUserProfileService'
];
/* @ngInject */
function WizardController(UserService,ActiveUserProfileService) {
var vm = this;
console.log("ActiveUserProfileService");
console.log(ActiveUserProfileService.isAccount("professional")); //is Returning me undefined
}
})
();
关键是,您试图在另一个函数(回调)中返回isAccount的值。当您这样做时,您将向这个函数返回一个值,而不是isAccount本身,因此isAccount将不会返回任何未定义的内容。
当您调用一个同步方法时,isAccount也必须是同步的
替换
function isAccount(accountName) {
UserService.getAuthenticatedUser()
.then(function (response) {
var data = response.data;
UserService.getUserInformation(data.user.id)
.then(function (response) {
var userDetails = response.data;
console.log("It is");
console.log(accountName == userDetails.account_types[0].description_internal);
return accountName == userDetails.account_types[0].description_internal;
});
})
}
与
当然,您还必须注入$q服务您看到回调了吗?假设返回,那么它不是主函数返回,而是回调。您必须创建一个承诺,该承诺将在调用回调时得到解决,然后您返回此承诺。我们缺少有关如何在控制器中使用该承诺的信息,您介意将控制器粘贴到此处吗?在控制器内部调用用户服务有效吗?是的。我已经编辑了控制器谢谢。LanaStf出色地回答了您的问题:)谢谢Lana,正在工作,只有一个问题是返回控制器中的“c{$$state:Object}”。不客气。这是一个承诺,就像getAuthenticatedUser返回一个一样。因此,您可以使用isAccount()。然后(yourlackwithbooleanhere);但是有没有可能从isAccount返回值whout“then..”,我这么说是因为在我插入的控制器中,code
vm.isAccount=function(account){ActiveUserProfileService.isAccount(account).then(function(result){return result;} ); };<代码>代码
然后我在ngIf的视图中调用了这个函数,但是给了我一个“error$rootScope:infdig Infinite$digest循环”你又犯了同样的错误,伙计。看看vm.isAccount return,它不会返回任何东西,对吗?内部返回语句用于回调,而不是主函数vm.isAccount。O returno nãO estáservindo para O método isAccount,mas para a callback passada dentro do then,ou seja,O método isAccount nãO estáretronaldo nada
function isAccount(accountName) {
var deferred = $q.defer();
UserService.getAuthenticatedUser()
.then(function (response) {
var data = response.data;
//when the user is loaded, then you resolve the promise you has already returned
UserService.getUserInformation(data.user.id)
.then(function (response) {
var userDetails = response.data;
console.log("It is");
console.log(accountName == userDetails.account_types[0].description_internal);
deferred.resolve(accountName == userDetails.account_types[0].description_internal);
return; //here is not isAccount return, but anonymous inner function 'function (response)', you got it?
});
});
return deferred.promise; //return as soon as creates the promise
}