【问题标题】:How to get current umbraco user details using js如何使用 js 获取当前 umbraco 用户详细信息
【发布时间】:2017-10-06 13:47:28
【问题描述】:

我想获取 umbraco 当前用户的详细信息(不是会员,只有用户/管理员)。我需要使用 JS 获取我的详细信息,我使用 userService,它返回 undefined.. 我不知道为什么... 有没有办法使用 ng & js 获取当前用户详细信息?

//js - 示例

angular.module('umbraco').controller('LoginController', [

    '$scope',
    '$http',
    'editorState',
    'contentResource',
    'userService',

    function ($scope, $http, editorState, contentResource, userService) {

        $scope.loginAsMember = function () {                                            
            var _memberId = editorState.current.id;
            $scope.curUser =  userService.getCurrentUser();                     
            );
        };     
    }
]);

【问题讨论】:

    标签: javascript c# angularjs umbraco


    【解决方案1】:

    如果您查看第 2170 行附近的“/Umbraco/Js/umbraco.contollers.js”,您可以看到 userService.getCurrentUser() 的使用情况。

    userService.getCurrentUser()返回一个promise对象,所以你可以使用.then()来设置你的$scope.user,然后做你需要做的事情。

    所以尝试类似...

    angular.module('umbraco').controller('LoginController', [
    
       '$scope',
       '$http',
       'editorState',
       'contentResource',
       'userService',
    
       function ($scope, $http, editorState, contentResource, userService) {
    
           $scope.loginAsMember = function () {
                  userService.getCurrentUser().then(function (user) {
                       var _memberId = editorState.current.id;
                       $scope.curUser = user;
                       console.log($scope.curUser.name);
                  });
           };
       }
    ]);
    

    当然,如果在您看来,如果您使用了{{curUser.name }},那么一旦用户对象可用,双向数据绑定就应该处理它。但是如果你想在你的 JS 中使用它,那么你需要特别注意在通过回调和承诺等使用它之前确保对象存在。

    【讨论】:

      猜你喜欢
      • 2016-10-30
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      相关资源
      最近更新 更多