angular/angular.js — pull request #2555 by redhotsly (2013-05-01 02:26:34+00:00)
Title: Fix 592/1699 prevent route change start

Hi, this is my first PR ever so please let me know if did not do things the *right way*. I signed the CLA.

It's my attempt at fixing: angular#1699 and angular#592

This change adds support to calling `preventDefault()` on `$route`'s `$routeChangeStart` event in order to cancel a change of route while leaving the browser's Url to its new location.

This is different from calling `event.preventDefault()` on `$location`'s `$locationChangeStart` because that reverts the browser's Url to its last Url.

Basic usage
----------------

```javascript
$rootScope.$on('$routeChangeStart', function(event, next, current) {
event.preventDefault();
});
```

Example: Blocking a specific route change
----------------

**Setup routes:**

```javascript
$routeProvider.when('/Book/new', { path: '/Book/new', templateUrl: 'partials/book.html', controller: 'BookControler'});
$routeProvider.when('/Book/:d', { path: '/Book/:d', templateUrl: 'partials/book.html', controller: 'BookControler'});
```
As you can see, I added a `path` attribute to my routes in order to compare them when `$routeChangeStart` will be fired. You could instead give a `name` such as "New Book" and "Existing Book" to your routes and compare using names.

**Cancel route:**
In a controller, cancel route change when going from /Book/new to /Book/:d:

```javascript
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if (current && current.path === "/Book/new" && next && next.path === "/Book/:d"){
event.preventDefault();
}
});
```

-------

What do you think about this solution? Do you see any potential conflicts with other parts? Can you think of additional test cases I should write?