1

I'm trying to make permission with $routeChangeStart in angular JS but it does't work. What I suppose to do is to make $routeChangeStart work with $stateChangeSuccess:

angular.module('app').run(['$rootScope','$location', '$state', '$stateParams', 'loginService', 
  function($rootScope, $location, $state, $stateParams, loginService) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
    var routespermission=['/'];
    $rootScope.$on('$routeChangeStart', function(){     
        alert('testing');
        console.log('>>exit tab: '+routespermission.index0f($location.path()));
        console.log('>>logged:' +loginService.islogged());
        if(routespermission.index0f($location.path()) !=-1 && !loginService.islogged()){
            $location.path('/login');
        }
    });
    $rootScope.$on('$stateChangeSuccess', function() {
        window.scrollTo(0, 0);
        console.log('testing');
    });
    FastClick.attach(document.body);

  },
])
georgeawg
  • 48,608
  • 13
  • 72
  • 95

1 Answers1

0

UI-Router, i want make the code could using on the UI-Router

Use $stateChangeStart instead of $routeChangeStart:

app.run(function($rootScope, $location, $state, $stateParams, loginService) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
    var routespermission=['/'];
    ̶$̶r̶o̶o̶t̶S̶c̶o̶p̶e̶.̶$̶o̶n̶(̶'̶$̶r̶o̶u̶t̶e̶C̶h̶a̶n̶g̶e̶S̶t̶a̶r̶t̶'̶,̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶)̶{̶ ̶
    $rootScope.$on('$stateChangeStart', function(){     
        alert('testing');
        console.log('>>exit tab: '+routespermission.index0f($location.path()));
        console.log('>>logged:' +loginService.islogged());
        if(routespermission.index0f($location.path()) !=-1 && !loginService.islogged()){
            $location.path('/login');
        }
    });
    $rootScope.$on('$stateChangeSuccess', function() {
        window.scrollTo(0, 0);
        console.log('testing');
    });
    FastClick.attach(document.body);
})

For more information, see UI-Router 0.3.1 Api Reference - $state.


StateChange events have been deprecated for ui.router >= 1.0.

For more information, see UI Router stateChangeSuccess event not firing.

georgeawg
  • 48,608
  • 13
  • 72
  • 95