File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -275,6 +275,11 @@ function $RouteProvider(){
275
275
* defined in `resolve` route property. Once all of the dependencies are resolved
276
276
* `$routeChangeSuccess` is fired.
277
277
*
278
+ * Call event.preventDefault() to prevent changing route but leave the browser Url
279
+ * to it's new location (as opposed to calling event.preventDefault() on $location's
280
+ * $locationChangeStart event which cancels changing the browser's Url).
281
+ *
282
+ *
278
283
* @param {Route } next Future route information.
279
284
* @param {Route } current Current route information.
280
285
*/
@@ -405,7 +410,9 @@ function $RouteProvider(){
405
410
$rootScope . $broadcast ( '$routeUpdate' , last ) ;
406
411
} else if ( next || last ) {
407
412
forceReload = false ;
408
- $rootScope . $broadcast ( '$routeChangeStart' , next , last ) ;
413
+ if ( $rootScope . $broadcast ( '$routeChangeStart' , next , last ) . defaultPrevented ) {
414
+ return ;
415
+ }
409
416
$route . current = next ;
410
417
if ( next ) {
411
418
if ( next . redirectTo ) {
Original file line number Diff line number Diff line change @@ -205,6 +205,28 @@ describe('$route', function() {
205
205
} ) ;
206
206
207
207
208
+ it ( 'should not change route when $routeChangeStart is canceled' , function ( ) {
209
+ module ( function ( $routeProvider ) {
210
+ $routeProvider . when ( '/somePath' , { template : 'some path' } ) ;
211
+ } ) ;
212
+ inject ( function ( $route , $location , $rootScope , $log ) {
213
+ $rootScope . $on ( '$routeChangeStart' , function ( event ) {
214
+ $log . info ( '$routeChangeStart' ) ;
215
+ event . preventDefault ( ) ;
216
+ } ) ;
217
+
218
+ $rootScope . $on ( '$routeChangeSuccess' , function ( event ) {
219
+ throw new Error ( 'Should not get here' ) ;
220
+ } ) ;
221
+
222
+ $location . path ( '/somePath' ) ;
223
+ $rootScope . $digest ( ) ;
224
+
225
+ expect ( $log . info . logs . shift ( ) ) . toEqual ( [ '$routeChangeStart' ] ) ;
226
+ } ) ;
227
+ } ) ;
228
+
229
+
208
230
describe ( 'should match a route that contains special chars in the path' , function ( ) {
209
231
beforeEach ( module ( function ( $routeProvider ) {
210
232
$routeProvider . when ( '/$test.23/foo*(bar)/:baz' , { templateUrl : 'test.html' } ) ;
You can’t perform that action at this time.
0 commit comments