Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Route continues to be processed even if providing a redirectTo #3332

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ngRoute/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,17 @@ 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();
} else {
$location.url(next.redirectTo(next.pathParams, $location.path(), $location.search()))
.replace();
}
if (url !== $location.url()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this if? I think that we should just return no? if you redirect to the same url then you get an infinite loop, but that's a programming error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IgorMinar its not necessarily a programming error. In my experience, I may use the redirect function to validate user has pre-qualifying conditions met to hit that area. If they don't, redirect them somewhere else. If they do, keep the course and continue on with the intended route.

It does not currently get caught in an infinite loop since the url didn't change, it doesn't fire any change watchers to re-evaluate.

return;//exit out and don't process current next value, wait for next location change from redirect
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/ngRoute/directive/ngViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down