【发布时间】: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,
})
【问题讨论】:
-
我有点认为你在处理这一切都是错误的。我在想的是你应该为医生和病人创建不同的状态,然后在你的 .run(或准备好)函数中检查用户并使用 $state.go。我相信这可以带你走向正确的方向stackoverflow.com/questions/27907670/…
-
@Marko 谢谢你,先生!我会看看这个
-
ui-router 没有为 templateURL 函数使用 $injector 服务。它使用
params参数调用该函数。有关详细信息,请参阅ui-router $stateProvider API Reference。期望$rootScope被注入该函数是错误的。 -
@Marko,我发布了我的解决方案:D