I have attached data attribute in each .state
to identify the user (authenticated or public) as following (one state example)
$stateProvider
.state('admin-panel.public.home', {
url: '/p',
templateUrl: 'app/public/home.tmpl.html',
controller: 'PublicHomeController',
controllerAs: 'ctrl',
data: {
requireLogin: false
}
});
I need to use the some state for both of user (authenticated and public) as an example
.state('403', {
url: '/403',
templateUrl: '403.tmpl.html',
controller: function($scope, $state, APP, Auth) {
$scope.app = APP;
$scope.goHome = function() {
if(Auth.isAuthenticated()){
$scope.requireLogin = true;
$state.go('admin-panel.default.home');
}
else{
$scope.requireLogin = false;
$state.go('admin-panel.public.home');
}
};
},
data: {
requireLogin: $scope.requireLogin
}
})
Here when the authenticated user access this state I need to pass the true value to requireLogin: true
as well when public user access this state I need to pass the false
value as requireLogin: false
. I checked the current user status in the controller
as above. How can I bind the $scope.requireLogin
to data attribute?
Anyone in expert of ui-router
please tell a way to solve???
You can solve your problem in a very cleaner way. Let's start with a global controller example
GlobalCtrl
which is added to thebody
orhtml
tag likeng-controller="GlobalCtrl
.Doing this will enable us to keep the scope of this
GlobalCtrl
throughout your single page Angular app (as you are usingui-router
) and we can avoid the usage of$rootScope
(actually mimicking the usage of$rootScope
).Now, inside your
GlobalCtrl
define something like this:Now, since
$scope
ofGlobalCtrl
is inbody
orhtml
then every state or directive will inherit the scope of thisGlobalCtrl
and then you simply have to check in your any controller of variable$scope.globalData.requireLogin
.