简体   繁体   English

角全栈获取用户ID

[英]Angular-fullstack get user id

I'm using angular-fullstack to build a single page application, and in one of my controllers I'm trying to assign a user's id to a variable. 我正在使用angular-fullstack构建单个页面应用程序,并且在我的一个控制器中,我试图将用户的id分配给变量。 The code 编码

$scope.getCurrentUser = Auth.getCurrentUser;

returns 回报

function () {
        return currentUser;
}

This works fine for displaying in my view as my angular code can interpret the function and display the user id using {{getCurrentUser()._id}} which I'm assuming evaluates the promise and displays the code to the view. 这对在我的视图中显示效果很好,因为我的角度代码可以解释功能并使用{{getCurrentUser()._id}}显示用户ID,我假设它会评估Promise并在视图中显示代码。

My question is how do I assign $scope.getCurrentUser = Auth.getCurrentUser; 我的问题是如何分配$scope.getCurrentUser = Auth.getCurrentUser; to a variable in my controller? 到控制器中的变量? Whenever I do so, I get undefined variables. 每当我这样做时,我都会得到未定义的变量。 I've tried: 我试过了:

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

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

I've read forum posts like this and this that explain that these methods are meant to be returned the way they are. 我读过论坛帖子像这样这个解释,这些方法都意味着要返回他们的方式。 Another post that is basically the same question as mine, but I'm still confused how to store the user id becuase the answer was hinting it was console.log that was the issue. 另一则帖子与我的问题基本相同,但是我仍然困惑如何存储用户ID,因为答案暗示这是问题的console.log。 Any help would be greatly appreciated, thanks. 任何帮助将不胜感激,谢谢。

Try the following and let me know how this works out: 请尝试以下操作,并让我知道其工作原理:

angular.module('yourModuleName').controller('YourController', ['$scope', 'Authentication',
    function($scope, Authentication) {

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

        console.log($scope.getId);

    }
]);

Make sure Authentication is included in your controller. 确保控制器中包含Authentication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM