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

Commit 96c71b0

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 e22c5d3 commit 96c71b0

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
@@ -183,11 +183,20 @@ function $RouteProvider() {
183183
* `redirectTo` takes precedence over `resolveRedirectTo`, so specifying both on the same
184184
* route definition, will cause the latter to be ignored.
185185
*
186+
* - `[reloadOnUrl=true]` - `{boolean=}` - reload route when any part of the URL changes
187+
* (inluding the path) even if the new URL maps to the same route.
188+
*
189+
* If the option is set to `false` and the URL in the browser changes, but the new URL maps
190+
* to the same route, then a `$routeUpdate` event is broadcasted on the root scope (without
191+
* reloading the route).
192+
*
186193
* - `[reloadOnSearch=true]` - `{boolean=}` - reload route when only `$location.search()`
187194
* or `$location.hash()` changes.
188195
*
189-
* If the option is set to `false` and url in the browser changes, then
190-
* `$routeUpdate` event is broadcasted on the root scope.
196+
* If the option is set to `false` and the URL in the browser changes, then a `$routeUpdate`
197+
* event is broadcasted on the root scope (without reloading the route).
198+
*
199+
* **Note:** This option has no effect if `reloadOnUrl` is set to false.
191200
*
192201
* - `[caseInsensitiveMatch=false]` - `{boolean=}` - match routes without being case sensitive
193202
*
@@ -202,6 +211,9 @@ function $RouteProvider() {
202211
this.when = function(path, route) {
203212
//copy original route object to preserve params inherited from proto chain
204213
var routeCopy = shallowCopy(route);
214+
if (angular.isUndefined(routeCopy.reloadOnUrl)) {
215+
routeCopy.reloadOnUrl = true;
216+
}
205217
if (angular.isUndefined(routeCopy.reloadOnSearch)) {
206218
routeCopy.reloadOnSearch = true;
207219
}
@@ -544,8 +556,9 @@ function $RouteProvider() {
544556
* @name $route#$routeUpdate
545557
* @eventType broadcast on root scope
546558
* @description
547-
* The `reloadOnSearch` property has been set to false, and we are reusing the same
548-
* instance of the Controller.
559+
* Any of the `reloadOnSearch` and `reloadOnUrl` properties has been set to false and we are
560+
* reusing the same instance of the route (including template, controller instance, resolved
561+
* dependencies etc).
549562
*
550563
* @param {Object} angularEvent Synthetic event object
551564
* @param {Route} current Current/previous route information.
@@ -653,9 +666,21 @@ function $RouteProvider() {
653666
var lastRoute = $route.current;
654667

655668
preparedRoute = parseRoute();
656-
preparedRouteIsUpdateOnly = preparedRoute && lastRoute && preparedRoute.$$route === lastRoute.$$route
657-
&& angular.equals(preparedRoute.pathParams, lastRoute.pathParams)
658-
&& !preparedRoute.reloadOnSearch && !forceReload;
669+
preparedRouteIsUpdateOnly =
670+
// IF this is not a forced reload
671+
!forceReload
672+
// AND both `lastRoute`/`preparedRoute` are defined
673+
&& preparedRoute && lastRoute
674+
// AND they map to the same Route Definition Object
675+
&& (preparedRoute.$$route === lastRoute.$$route)
676+
// AND `reloadOnUrl` is disabled
677+
&& (!preparedRoute.reloadOnUrl
678+
// OR `reloadOnSearch` is disabled
679+
|| (!preparedRoute.reloadOnSearch
680+
// AND both routes have the same path params
681+
&& angular.equals(preparedRoute.pathParams, lastRoute.pathParams)
682+
)
683+
);
659684

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

0 commit comments

Comments
 (0)