问题描述
背景:我正在学习 AngularJS,我编写了示例代码来为模块创建服务,并将数据从服务传递到模块的控制器之一,一切正常.
Background: I'm learning AngularJS, I've written sample code to create a service for a module, and pass the data from service into one of the module's controller, all works fine.
但是昨天当我尝试将以下服务的数据传递给控制器时,它只是显示数据未定义,我不知道为什么.
But yesterday when I tried to pass the following service's data into a controller, it just show the data is undefined, and I don't know why.
var notif = angular.module('testnotif',[]);
notif.service('sharedUsers', ['$http', function($http){
var url = current_dir+'/testNotif/getAllUsersjson';
var allUsers = [];
var getAll = function (){
$http.get(url)
.success(function(data, status) {
data.forEach(function(user){
allUsers[user['ys_user_id']]=user;
});
console.log('in service');
console.log(allUsers);//here console shows allUsers data correctly fetched from server.
return allUsers;
});
};
return {
getAll: getAll
};
}]);
notif.controller('notifCtrl',['$scope','$http', '$timeout','sharedUsers',
function($scope, $http, $timeout,sharedUsers ){
$scope.allUsers=sharedUsers.getAll(); //shareUsers.getAll() returns nothing
console.log('in notifCtrl');
console.log($scope.allUsers); //console just prints 'undefined'. what goes wrong?
}]);
该服务有一个 getAll 属性,它是一个获取 allUsers 数据的函数.在控制台中,它显示 allUsers 已经填充了来自服务器的数据,但是在控制器中,当我调用这个 getAll() 函数时,它什么都不返回.我的代码有什么问题?非常感谢!
the service has a getAll property which is a function get allUsers data.in the console it shows allUsers are populated with data from server already,but in the controller, when I call this getAll() function, it returns nothing.what is the problem with my code?thank you very much!
现在我得到了正确的数据,感谢@doodeec 指出返回"错误,并建议使用promise api.并感谢@IntegrityFirst 指出了注入常量的更好方法.以下代码是工作版本,以防万一有人想知道.
now I got the data correct, thanks to @doodeec for pointing out the 'return' error, and suggesting using promise api. and thanks to@IntegrityFirst points out a better way for injecting constant. the following code is the working version, just in case anyone wants to know.
//define constant:
notif.value('current_dir', 'dir to current path');
//in module, define service
notif.service('sharedUsers', ['$http','$q','current_dir', function($http, $q, current_dir){
var url = current_dir+'/testNotif/getAllUsersjson';
var allUsers = [];
var getAll = function (){
var deferred = $q.defer();
$http.get(url)
.success(function(data, status) {
data.forEach(function(user){
allUsers[user['ys_user_id']]=user;
});
console.log('in service');
console.log(allUsers);
deferred.resolve(allUsers);
});
return deferred.promise;
};
return {
getAll: getAll
};
}]);
//in controller
notif.controller('notifCtrl',['$scope','$http', '$timeout','sharedUsers','$q',
function($scope, $http, $timeout,sharedUsers,$q ){
sharedUsers.getAll().then(function(data){$scope.allUsers=data;});
}]);
推荐答案
getAll
是不返回任何内容的函数,这就是为什么 $scope.allUsers
未定义
getAll
is function which doesn't return anything, that's why $scope.allUsers
is undefined
你可能想这样做
controller - 当 getAll
返回的承诺被解析时,代码将等待,并将该承诺返回的数据分配给范围变量
controller - code will wait while promise returned from getAll
is resolved, and assign the data returned from that promise to scope variable
sharedUsers.getAll().then(function(data) { $scope.allUsers = data; });
service - $http.get
返回 promise,因此您可以在方法调用中返回它,这样您就可以对 resolve/reject 在控制器中
service -
$http.get
returns promise, so you can return it in method call, so you will be able to react on resolve/reject
in the controller
var getAll = function (){
return $http.get(url)
.success(function(data, status) {
data.forEach(function(user){
allUsers[user['ys_user_id']]=user;
});
return allUsers;
});
};
这篇关于Angular 控制器无法从服务中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!