Skip to content

Instantly share code, notes, and snippets.

@vojtajina
Created July 1, 2011 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vojtajina/1058740 to your computer and use it in GitHub Desktop.
Save vojtajina/1058740 to your computer and use it in GitHub Desktop.
Angular - cancelling $route change
<!doctype html>
<html xmlns:ng="http://angularjs.org">
<script src="http://code.angularjs.org/0.9.17/angular-0.9.17.js" ng:autobind></script>
<body>
<script>
function BookCntl() {
this.name = "BookCntl";
}
function ChapterCntl() {
this.name = "ChapterCntl";
}
function Main($route, $location) {
var lastRoute;
$route.when('/Book/:bookId', {template:'content1.html', controller:BookCntl});
$route.when('/Book/:bookId/ch/:chapterId', {template:'content2.html', controller:ChapterCntl});
$route.onChange(function() {
if (!lastRoute || confirm('Are you sure ?')) {
lastRoute = $route.current;
$route.current.scope.params = $route.current.params;
} else {
// cancel the route change
$route.current = lastRoute;
}
});
this.$route = $route;
this.$location = $location;
}
Main.$inject = ['$route', '$location'];
</script>
<div ng:controller="Main">
<ng:include src="$route.current.template" scope="$route.current.scope"/>
</div>
</body>
</html>
Simple content 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment