【问题标题】:Access Current User Info from Angular App从 Angular 应用程序访问当前用户信息
【发布时间】:2016-05-24 20:48:38
【问题描述】:

我的应用程序前端是 Angular SPA,后端是 asp.net webapi。我在从应用程序的其他部分访问用户个人资料信息时遇到问题:

当用户登录时,必须可以从应用程序的所有部分访问配置文件信息。我将基本配置文件信息放在作为顶级控制器的 homeController 中的一个对象中,但是当我从子控制器访问此对象时,在使用其中一个用户配置文件属性时出现“未定义”错误,尽管我可以获得整个对象.当我使用console.log() 显示对象时,没关系,所有属性都在那里,但是当console.log() 显示其中一个属性时,控制台显示“未定义”或显示在那里。我做错了什么?

我的代码:

在 homeController 中(在 index.html 的选项卡中定义):

$scope.currentUser = authService.authentication;

console.log($scope.currentUser); // shows all properties ok.

在 userDashController 中(在 app.js 中的路由配置访问的部分视图中定义):

$scope.fromCurrentUser = $scope.currentUser;

console.log($scope.fromCurrentUser); // show all properties ok

console.log($scope.fromCurrentUser.userAvatarImgUrl); // blank in the console

var comaId = $scope.currentUser.orgId;

console.log(comaId); // blank in the console

即使我尝试注入 authService,它在用户登录时设置用户配置文件,我也遇到了类似的问题。或者我尝试使用 $rootScope,但仍然无法正常工作。

不知道我做错了什么(我是 Angular 的新手,才几个月)

请帮忙。提前致谢

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    你用过 $localStorage 吗?我在使用 firebase 时遇到了 $rootScope 的问题。但是如果你使用 $rootScope 来保持用户身份验证并使用它来检查它是否不为空,它会发现隐藏了其他功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 2020-09-17
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 2019-04-18
      • 2020-05-09
      相关资源
      最近更新 更多