javascript - Why angular redirects to the route but loads the wrong view -
i have basic need wanted address routes:
a bunch of routes defined if user not authenticated, he's redirected login pagemy problem that, first time web app displayed, user seem correctly redirected because the uri correct, not displayed view (it corresponds default view).
i've created jsbin that, if prefer see real. result can viewed here.
steps reproduce:
load page you see needsauth, meaning content of viewneedsauth
displayed if uri contains login (which corresponds view login
) reload page you see 'login', meaning content of view login
the html code:
<!doctype html> <html ng-app='app'> <head> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script> <meta charset="utf-8"> <title>js bin</title> </head> <body> <div ng-view> </div> </body> </html>
here javascript code:
var template = '{{vm.text}}'; angular .module('app', ['ngroute']) .config(['$routeprovider', routes]) .controller('needsauthcontroller', needsauthcontroller) .controller('logincontroller', logincontroller) .run(['$rootscope', '$location', registerredirection]); function routes($routeprovider){ $routeprovider .when('/needsauth', { controller: 'needsauthcontroller', controlleras: 'vm', template: template }) .when('/login', { controller: 'logincontroller', controlleras: 'vm', template: template }) .otherwise({ redirectto: '/needsauth' }); } function needsauthcontroller(){ var vm = this; vm.text = 'needsauth'; } function logincontroller(){ var vm = this; vm.text = 'login'; } function registerredirection($rootscope, $location){ $rootscope.$on('$routechangestart', function(event, next){ $location.path('/login'); }); }
on first load, can see uri , content not match
if reload page, working expected:
is there i'm doing wrong? if it's case, right way redirect user under conditions?
[edit]: angular 1.2.26 seems behave correctly in case, not angular 1.3.2
change
function registerredirection($rootscope, $location){ $rootscope.$on('$routechangestart', function(event, next){ $location.path('/login'); }); }
to
function registerredirection($rootscope, $location){ $rootscope.$on('$locationchangestart', function(event, next){ $location.path('/login'); }); }
and initial load works correctly.
javascript angularjs angularjs-routing
No comments:
Post a Comment