【发布时间】:2015-08-22 14:49:51
【问题描述】:
我正在编写我的第一个 AngularJs 应用程序。应用需要知道不同控制器中的当前用户。当前用户通过休息调用检索。首先,我只是使用 $scope 在页面顶部添加了当前用户信息:
var currentUser = {};
app.controller('currentUserCtrl', function($scope, $http) {
$scope.currentUser = null;
$http.get(rootUrl + '/timetracker/user/current').success(
function(response) {
$scope.currentUser = response;
currentUser = response;
});
}
);
控制器工作正常,但其他控制器无法访问“currentUser”。我的第二次尝试(在阅读了全局变量之后)是使用 $rootScope。所以我更改了上面的$rootScope.currentUser = response; 并省略了var currentUser。我将$rootScope 添加到我的所有控制器中,并尝试获取currentUser.id。但这也不起作用——用户对象为空。我的第三次尝试是使用工厂:
app.factory('currentUser', function($http){
var currentUser = {};
$http.get(rootUrl + '/timetracker/user/current').success(
function(response) {
currentUser = response;
});
return currentUser;
});
我的控制器现在注入“currentUser”,但这也是空值(我想要获取的所有值都是“未定义”)。那么如何让当前用户全局可用呢?
难道是在异步调用和写入变量成功之前使用了currentUser?如何确定 currentUser 调用已完成?
编辑 使用 currentUser 的控制器:
app.controller('createBookingCtrl', function($scope, $http, $filter, currentUser) {
//list of projects for current user (usersprojects)
$scope.projectsList = {};
$http.get(rootUrl + '/timetracker/usersprojects/user/' + currentUser.id).success(function(response) {
for (var i = 0; i < response.length; ++i)
//map users project by projects name
$scope.projectsList[response[i].project.name] = response[i];
});
});
currentUser.id 的值未定义。
第二次编辑
我刚刚使用 js 调试器进行了测试:工厂内currentUser = response 在其他控制器访问该值之后运行。那么如何确定/等待响应呢?
【问题讨论】:
-
您能否附上您用于检查 currentUser 值(未定义的位置)的控制器代码?
-
通过您的第三种方法,您如何访问控制器中的工厂变量。可以发在这里吗
-
你必须像这样访问它,因为它返回一个承诺数据。 $scope.userId = currentUser.getUser().then(function(result) { return result.id ; });
-
好力谢谢 - 你能回答一下吗 - 我会在此期间尝试......
标签: angularjs