Angular-fullstack获取用户ID

时间:2015-05-17 20:29:30

标签: angularjs node.js express mean-stack angular-fullstack

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

$scope.getCurrentUser = Auth.getCurrentUser;

返回

function () {
        return currentUser;
}

这适用于在我的视图中显示,因为我的角度代码可以解释函数并使用{{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

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

1 个答案:

答案 0 :(得分:0)

尝试以下操作,让我知道这是如何解决的:

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

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

        console.log($scope.getId);

    }
]);

确保控制器中包含Authentication