Created
July 1, 2011 15:07
-
-
Save vojtajina/1058740 to your computer and use it in GitHub Desktop.
Angular - cancelling $route change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Simple content 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Content2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment