javascript - AngularJS - Need some combination of $routeChangeStart and $locationChangeStart -
my problem similar 1 found here:
angularjs - cancel route change event
in short, i'm using $routechangestart , trying change current route using $location. when do, console shows me original page still loads , overwritten new page.
the solution provided use $locationchangestart instead of $routechangestart, should work preventing redirect. unfortunately, i'm using additional data in $routeprovider need access while changing route (i use track page restrictions). here's example...
$routeprovider. when('/login', { controller: 'loginctrl', templateurl: '/app/partial/login.html', access: false}). when('/home', { controller: 'homectrl', templateurl: '/app/partial/home.html', access: true}). otherwise({ redirectto: '/login' }); $rootscope.$on('$routechangestart', function(event, next, current) { if(next.access){ //do stuff } else{ $location.path("/login"); //this load current route first (ie: '/home'), , //redirect user correct 'login' route. } });
with $routechangestart, can use "next" , "current" parameters (see angularjs - $route) objects retrieve 'access' values. $locationchangestart, 2 parameters return url strings, not objects. there seems no way retrieve 'access' values.
is there way can combine redirect-stopping power of $locationchangestart object-flexibility of $routechangestart achieve need?
one approach comes mind trying use resolve parameter this:
var resolver = function(access) { return { load: function($q) { if (access) { // fire $routechangesuccess var deferred = $q.defer(); deferred.resolve(); return deferred.promise; } else { // fire $routechangeerror return $q.reject("/login"); } } } } $routeprovider. when('/login', { controller: 'loginctrl', templateurl: '/app/partial/login.html', resolve: resolver(false)}). when('/home', { controller: 'homectrl', templateurl: '/app/partial/home.html', resolve: resolver(true)}). otherwise({ redirectto: '/login' });
please note haven't tested code above i'm doing similar stuff in projects.
Comments
Post a Comment