背景:我正在学习AngularJS,我已经编写了用于为模块创建服务的示例代码,并将服务中的数据传递到模块的控制器之一中,所有工作都正常。角度控制器无法从服务中获取数据
但昨天当我试图将下列服务的数据传递给控制器时,它只是显示数据是未定义的,我不知道为什么。
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数据。 它在控制台显示所有用户已填充来自服务器的数据, 但在控制器中,当我调用此getAll()函数时,它不返回任何内容。 我的代码有什么问题? 非常感谢你!
编辑︰现在我得到的数据正确,感谢@doodeec指出'返回'错误,并建议使用承诺api。并感谢@ IntegrityFirst指出了一个更好的注入常量的方法。以下代码是工作版本,以防万一谁想知道。
//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;});
}]);
我注意到,您正在使用的看起来像它的变量current_dir在全球范围内分配。一个重要的AngularJS最佳实践是避免将对象和变量放入全局范围。您可以使用提供者特别是[值提供者配方](http://docs.angularjs.org/guide/providers#value-recipe)将current_dir注入到服务中。在你的应用程序中它会看起来像'notif.value('current_dir','/ app/directory');'。然后在注入器中为你的服务注入current_dir到服务中。'['$ http','current_dir',函数($ http,current_dir){' – BrightIntelDusk
感谢@IntegrityFirst指出它。是的,我应该将常量注入模块,而不是使用全局范围。我会根据你的建议修改我的代码。 :) –