how to bind the relevant value in case of current status in ui-router?

I have attached data attribute in each .state to identify the user (authenticated or public) as following (one state example)

 .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) {
        $ = APP;

        $scope.goHome = function() {
                $scope.requireLogin = true;
                $scope.requireLogin = false;
    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 the body or html tag like ng-controller="GlobalCtrl.

Doing this will enable us to keep the scope of this GlobalCtrl throughout your single page Angular app (as you are using ui-router) and we can avoid the usage of $rootScope (actually mimicking the usage of $rootScope).

Now, inside your GlobalCtrl define something like this:

// Using an object to avoid the scope inheritance problem of Angular
$scope.globalData = {};

// Will be called everytime before you start navigating to any state
$scope.$on('$stateChangeStart', function(event, toState, toParams) {
    $scope.globalData.requireLogin = false;
    var statesToLoginCheck = ['403', 'foo', 'bar']; // and other states on which you want to check if user is logged in or not

    // If the current state on which we are navingating is allowed to check for login
    if (statesToLoginCheck.indexOf( > -1) {
        if (Auth.isAuthenticated()) {
            $scope.globalData.requireLogin = true;
        } else {
            $scope.globalData.requireLogin = false;


Now, since $scope of GlobalCtrl is in body or html then every state or directive will inherit the scope of this GlobalCtrl and then you simply have to check in your any controller of variable $scope.globalData.requireLogin.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.