首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular-fullstack获取用户id

Angular-fullstack获取用户id
EN

Stack Overflow用户
提问于 2015-05-18 04:29:30
回答 1查看 2.6K关注 0票数 1

我正在使用angular-fullstack构建一个单页面应用程序,并且在我的一个控制器中,我试图将用户的id赋给一个变量。代码

$scope.getCurrentUser = Auth.getCurrentUser;

返回

代码语言:javascript
复制
function () {
        return currentUser;
}

这对于在我的视图中显示效果很好,因为我的angular代码可以解释函数并使用{{getCurrentUser()._id}}显示用户id,我假设它会评估promise并将代码显示到视图中。

我的问题是如何将$scope.getCurrentUser = Auth.getCurrentUser;赋值给控制器中的变量?每当我这样做的时候,我都会得到未定义的变量。我试过了:

代码语言:javascript
复制
$scope.getId = Auth.getCurrentUser()._id;
console.log($scope.getId); //undefined

$scope.getId = Auth.getCurrentUser();
console.log($scope.getId._id); //undefined

我读过像thisthis这样的论坛帖子,它们解释说这些方法就是按原样返回的。另一个post基本上和我的问题是一样的,但我仍然对如何存储用户id感到困惑,因为答案是暗示这是console.log的问题。任何帮助都将非常感谢,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-05-18 04:37:24

尝试以下操作,并让我知道它是如何工作的:

代码语言:javascript
复制
angular.module('yourModuleName').controller('YourController', ['$scope', 'Authentication',
    function($scope, Authentication) {

        var authentication = Authentication;
        $scope.getId = authentication.user._id;

        console.log($scope.getId);

    }
]);

确保您的控制器中包含Authentication

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30291821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档