From ccf5df620a1fe2792c520140f76310146fbd817b Mon Sep 17 00:00:00 2001 From: yoerlemans Date: Fri, 3 Jan 2014 14:10:54 +0100 Subject: [PATCH] Update route.js With this change it is possible to cancel a locationChange based on route data. The problem that I am trying to fix here is described in https://github.com/angular/angular.js/issues/5581 --- src/ngRoute/route.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index 34f3f9ec05f1..0d3c3b455aff 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -421,6 +421,23 @@ function $RouteProvider(){ * instance of the Controller. */ + /** + * @ngdoc event + * @name ngRoute.$route#beforeRouteChange + * @eventOf ngRoute.$route + * @eventType broadcast on root scope + * @description + * Broadcasted at the same time as locationChangeStart, passes through the event so the locationChange can be cancelled. + * Passes two other objects that represent the Routes of the current location and the one of the possible next. + * + * `$beforeRouteChange` is fired. + * + * @param {Object} angularEvent Synthetic event object. + * @param {Route} currentRoute current route information. + * @param {Route} nextRoute route information of the future route. + * @param {Object} angularEvent Synthetic event object of locationChangeStart. + */ + var forceReload = false, $route = { routes: routes, @@ -445,6 +462,13 @@ function $RouteProvider(){ $rootScope.$on('$locationChangeSuccess', updateRoute); + $rootScope.$on("$locationChangeStart", function (event, next, current) { + var nextRoute = parseRoute(), + lastRoute = $route.current; + + $rootScope.$broadcast('$beforeRouteChange', lastRoute, nextRoute, event); + }); + return $route; /////////////////////////////////////////////////////