how to bind the relevant value in case of current

2019-04-14 22:32发布

问题:

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???

回答1:

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
// https://github.com/angular/angular.js/wiki/Understanding-Scopes
$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(toState.name) > -1) {
        if (Auth.isAuthenticated()) {
            $scope.globalData.requireLogin = true;
            $state.go('admin-panel.default.home');
        } else {
            $scope.globalData.requireLogin = false;
            $state.go('admin-panel.public.home');
        }

        event.preventDefault();
        return;
    }
});

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.