AngularJS - Routes and Resolving
HTML
<script src="http://code.angularjs.org/1.1.4/angular.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
<div ng-app="myapp">
<div ng-view></div>
<a href="#" target="_blank">Open in Tab/Window</a>
<script type="text/ng-template" id="/list.html">
<h4>List</h4>
<a href="#/detail/1" class="btn">One</a>
<a href="#/detail/2" class="btn">Two</a>
</script>
<script type="text/ng-template" id="/detail.html">
<h4>Detail {{data.id}}</h4><a ng-click="back()" class="btn">Back</a>
</script>
</div>
JavaScript
var myapp = angular.module('myapp', []).config(function ($routeProvider) {
$routeProvider.when('/list', {
templateUrl: '/list.html'
}).when('/detail/:id/', {
templateUrl: '/detail.html',
controller: 'DetailCtrl'
}).otherwise({
redirectTo: '/list'
});
});
myapp.controller('DetailCtrl', function ($scope, $location) {
$scope.back = function () {
$location.path('/list');
};
});
myapp.run(function ($rootScope,$location) {
$rootScope.$on('$routeChangeStart', function(evt, absNewUrl, absOldUrl) {
console.log('aa'+absNewUrl);
//here you can check for your own condition and if not logged in then set $location.path(loginpath);
});
});