angularjs - Angular-fullstack 获取用户 ID

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

我正在使用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/

相关文章:

javascript - 鲍尔 "Git not in the PATH"错误

node.js - NodeJS Express - 渲染页面自动更新

javascript - Express - 更新对数据库MongoDB不生效

javascript - 如何根据angular js中的表数组获取下拉列表中的列?

javascript - UI-Router 状态 : How to make app. 使用 $rootScope.$ 在缩小安全上运行

javascript - 在 Angular 1 应用程序中使用 Angular 2 组件,无需升级插件

mysql - Nodejs异步: return mysql query result in for loop

javascript - 使用 ng-class 时,setTimeout 在表单更改之前不会执行

node.js - 将 HashMap 保存到 Mongoose ?

node.js - 如何在返回 Promise 的同时添加 catch