我正在使用angular-fullstack构建一个单页面应用程序,并且在我的一个控制器中,我试图将用户的id赋给一个变量。代码
$scope.getCurrentUser = Auth.getCurrentUser;
返回
function () {
return currentUser;
}这对于在我的视图中显示效果很好,因为我的angular代码可以解释函数并使用{{getCurrentUser()._id}}显示用户id,我假设它会评估promise并将代码显示到视图中。
我的问题是如何将$scope.getCurrentUser = Auth.getCurrentUser;赋值给控制器中的变量?每当我这样做的时候,我都会得到未定义的变量。我试过了:
$scope.getId = Auth.getCurrentUser()._id;
console.log($scope.getId); //undefined
$scope.getId = Auth.getCurrentUser();
console.log($scope.getId._id); //undefined我读过像this和this这样的论坛帖子,它们解释说这些方法就是按原样返回的。另一个post基本上和我的问题是一样的,但我仍然对如何存储用户id感到困惑,因为答案是暗示这是console.log的问题。任何帮助都将非常感谢,谢谢。
发布于 2015-05-18 04:37:24
尝试以下操作,并让我知道它是如何工作的:
angular.module('yourModuleName').controller('YourController', ['$scope', 'Authentication',
function($scope, Authentication) {
var authentication = Authentication;
$scope.getId = authentication.user._id;
console.log($scope.getId);
}
]);确保您的控制器中包含Authentication。
https://stackoverflow.com/questions/30291821
复制相似问题