【问题标题】:Different tabs for different user roles using $rootScope in .state在 .state 中使用 $rootScope 的不同用户角色的不同选项卡
【发布时间】:2017-03-20 00:10:08
【问题描述】:

我的目标是为不同的用户提供不同的标签布局 像病人和医生一样

在我的控制器中,我将登录的用户存储在 $rootScope.currentUser 中,如下所示

$auth.login(credentials).then(function(data) {
           return $http.get(CONSTANTS.LINK+'/authenticate/user');
        }, function(data) {
            var alertPopup = $ionicPopup.alert({
            title: 'Error Logging in',
            template: 'Invalid Credentials'
            });
            alertPopup.then(function(res) {
            });
        }).then(function(response) {
                var user = JSON.stringify(response.data.user);
                localStorage.setItem('user', user);
                $rootScope.authenticated = true;
                $rootScope.currentUser = response.data.user;
                $state.go('tabs.home');
            });; 

现在这是我的 app.js。什么都没发生。我做对了吗? 当我尝试 console.log($rootScope.currentUser);

时,$rootscope.currentUser 也会返回 undefined
.state('tabs', {
        url: '/tab',
        templateUrl: function ($rootScope) {
          if($rootScope.currentUser.role == 'Patient') {
            return 'templates/tabs.html';
          } else if ($rootScope.currentUser.role == 'Doctor') {
            return 'templates/tabs-doctors.html';
          }
        },
        abstract: true,
    })

【问题讨论】:

标签: angularjs ionic-framework


【解决方案1】:

由于我在他登录时将用户数据存储在本地存储中,因此我使用该数据来检查用户的角色。

.state('tabs', {
        url: '/tab',
        templateUrl: function () {
          var user = JSON.parse(localStorage.getItem('user'));
          role = user.role;
          if(role == 'Patient') {
            return 'templates/tabs.html';
          } else if (role == 'Doctor') {
            return 'templates/tabs2.html';
          } else {
            return 'templates/tabs.html';
          }
        },
        abstract: true,
    })

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 2012-04-14
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2013-07-22
    • 1970-01-01
    相关资源
    最近更新 更多