我正在使用angular-fullstack构建一个单页应用程序,在我的一个 Controller 中,我尝试将用户的 id 分配给变量。代码
$scope.getCurrentUser = Auth.getCurrentUser;
返回
function () {
return currentUser;
}
这对于在我的 View 中显示效果很好,因为我的 Angular 代码可以解释该函数并使用 {{getCurrentUser()._id}}
显示用户 id,我假设它评估 promise 并将代码显示到 View 。
我的问题是如何将 $scope.getCurrentUser = Auth.getCurrentUser;
分配给 Controller 中的变量?每当我这样做时,我都会得到 undefined variable 。我试过:
$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 的问题。任何帮助将不胜感激,谢谢。
最佳答案
尝试以下操作并让我知道效果如何:
angular.module('yourModuleName').controller('YourController', ['$scope', 'Authentication',
function($scope, Authentication) {
var authentication = Authentication;
$scope.getId = authentication.user._id;
console.log($scope.getId);
}
]);
确保您的 Controller 中包含身份验证
。
关于angularjs - Angular-fullstack 获取用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291821/