AngularJS Route To Target Views
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng:app="MyApp">
<script type="text/ng-template" id="1.html">
One View
</script>
<script type="text/ng-template" id="2.html">
Two View
</script>
<div ng-controller="MainCntl">
<a href="#/1">One</a> |
<a href="#/2">Two</a>
<hr />
<!-- somehow target this view with books -->
<div ng-view></div>
</div>
</div>
JavaScript
angular.module('MyApp', [], function($routeProvider, $locationProvider) {
$routeProvider.when('/1', {
template: '1.html',
controller: OneCntl
}).when('/2', {
template: '2.html',
controller: TwoCntl
}).otherwise({
redirectTo: '/1'
});
}).run(function($rootScope, $location) {
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
if (angular.isDefined(current) && angular.isDefined(current.$route) && !confirm('are you sure ?')) {
window.history.back();
}
});
});
function MainCntl($scope) { }
function OneCntl($scope) { }
function TwoCntl($scope) { }
window.onbeforeunload = function() {
var ans = confirm("Are you sure?");
if (ans) {
alert('yes');
}
};