diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index de0d90160353..6875bb11baaa 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -423,6 +423,7 @@ function $RouteProvider(){ $route.current = next; if (next) { if (next.redirectTo) { + var url = $location.url(); if (isString(next.redirectTo)) { $location.path(interpolate(next.redirectTo, next.params)).search(next.params) .replace(); @@ -430,6 +431,9 @@ function $RouteProvider(){ $location.url(next.redirectTo(next.pathParams, $location.path(), $location.search())) .replace(); } + if (url !== $location.url()) { + return;//exit out and don't process current next value, wait for next location change from redirect + } } } diff --git a/test/ngRoute/directive/ngViewSpec.js b/test/ngRoute/directive/ngViewSpec.js index 50531c18df58..1db91c24f0a0 100644 --- a/test/ngRoute/directive/ngViewSpec.js +++ b/test/ngRoute/directive/ngViewSpec.js @@ -509,6 +509,28 @@ describe('ngView', function() { }); }); + it('should not instantiate controller of redirected route', function() { + var firstController = jasmine.createSpy('first controller spy'); + var secondController = jasmine.createSpy('second controller spy'); + module(function($routeProvider) { + $routeProvider.when('/redirect', { + template: 'redirect view', + redirectTo: '/redirected', + controller: firstController + }); + $routeProvider.when('/redirected', { + template: 'redirected view', + controller: secondController + }); + }); + inject(function($route, $location, $rootScope) { + $location.path('/redirect'); + $rootScope.$digest(); + expect(firstController).not.toHaveBeenCalled(); + expect(secondController).toHaveBeenCalled(); + }); + }); + describe('ngAnimate ', function() { var window, vendorPrefix; var body, element, $rootElement;