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

Commit 4f54a92

Browse files
committed
feat($route): add support for the reloadOnUrl configuration option
Enables users to specify that a particular route should not be reloaded after a URL change (including a change in `$location.path()`), if the new URL maps to the same route. The default behavior is still to always load the matched route when any part of the URL changes. Related to #1699, #5860, #14999 (potentially closing the first two). Fixes #7925
1 parent 5fc9933 commit 4f54a92

File tree

2 files changed

+392
-35
lines changed

2 files changed

+392
-35
lines changed

src/ngRoute/route.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,20 @@ function $RouteProvider() {
180180
* `redirectTo` takes precedence over `resolveRedirectTo`, so specifying both on the same
181181
* route definition, will cause the latter to be ignored.
182182
*
183+
* - `[reloadOnUrl=true]` - `{boolean=}` - reload route when any part of the URL changes
184+
* (inluding the path) even if the new URL maps to the same route.
185+
*
186+
* If the option is set to `false` and the URL in the browser changes, but the new URL maps
187+
* to the same route, then a `$routeUpdate` event is broadcasted on the root scope (without
188+
* reloading the route).
189+
*
183190
* - `[reloadOnSearch=true]` - `{boolean=}` - reload route when only `$location.search()`
184191
* or `$location.hash()` changes.
185192
*
186-
* If the option is set to `false` and url in the browser changes, then
187-
* `$routeUpdate` event is broadcasted on the root scope.
193+
* If the option is set to `false` and the URL in the browser changes, then a `$routeUpdate`
194+
* event is broadcasted on the root scope (without reloading the route).
195+
*
196+
* **Note:** This option has no effect if `reloadOnUrl` is set to false.
188197
*
189198
* - `[caseInsensitiveMatch=false]` - `{boolean=}` - match routes without being case sensitive
190199
*
@@ -199,6 +208,9 @@ function $RouteProvider() {
199208
this.when = function(path, route) {
200209
//copy original route object to preserve params inherited from proto chain
201210
var routeCopy = shallowCopy(route);
211+
if (angular.isUndefined(routeCopy.reloadOnUrl)) {
212+
routeCopy.reloadOnUrl = true;
213+
}
202214
if (angular.isUndefined(routeCopy.reloadOnSearch)) {
203215
routeCopy.reloadOnSearch = true;
204216
}
@@ -540,8 +552,9 @@ function $RouteProvider() {
540552
* @name $route#$routeUpdate
541553
* @eventType broadcast on root scope
542554
* @description
543-
* The `reloadOnSearch` property has been set to false, and we are reusing the same
544-
* instance of the Controller.
555+
* Any of the `reloadOnSearch` and `reloadOnUrl` properties has been set to false and we are
556+
* reusing the same instance of the route (including template, controller instance, resolved
557+
* dependencies etc).
545558
*
546559
* @param {Object} angularEvent Synthetic event object
547560
* @param {Route} current Current/previous route information.
@@ -649,9 +662,21 @@ function $RouteProvider() {
649662
var lastRoute = $route.current;
650663

651664
preparedRoute = parseRoute();
652-
preparedRouteIsUpdateOnly = preparedRoute && lastRoute && preparedRoute.$$route === lastRoute.$$route
653-
&& angular.equals(preparedRoute.pathParams, lastRoute.pathParams)
654-
&& !preparedRoute.reloadOnSearch && !forceReload;
665+
preparedRouteIsUpdateOnly =
666+
// IF this is not a forced reload
667+
!forceReload
668+
// AND both `lastRoute`/`preparedRoute` are defined
669+
&& preparedRoute && lastRoute
670+
// AND they map to the same Route Definition Object
671+
&& (preparedRoute.$$route === lastRoute.$$route)
672+
// AND `reloadOnUrl` is disabled
673+
&& (!preparedRoute.reloadOnUrl
674+
// OR `reloadOnSearch` is disabled
675+
|| (!preparedRoute.reloadOnSearch
676+
// AND both routes have the same path params
677+
&& angular.equals(preparedRoute.pathParams, lastRoute.pathParams)
678+
)
679+
);
655680

656681
if (!preparedRouteIsUpdateOnly && (lastRoute || preparedRoute)) {
657682
if ($rootScope.$broadcast('$routeChangeStart', preparedRoute, lastRoute).defaultPrevented) {

0 commit comments

Comments
 (0)