app.config(['$routeProvider', '$locationProvider', function($routeProvider,$locationProvider){
$routeProvider
.when('/news/:id',{
templateUrl: 'html/article.html',
controller: 'articleCtrl',
controllerAs: 'article',
title: 'Artículo'
})
.when('/news/:id/sponsor',{
templateUrl: 'html/sponsor.html',
controller: 'sponsorCtrl',
controllerAs: 'sponsor',
title: 'Patrocinador'
});
$locationProvider.html5Mode(true).hashPrefix('!');
}]);
app.controller('sponsorCtrl', ['$scope','$interval','$location','$routeParams',
function($scope, $interval, $location, $routeParams) {
var promise;
$scope.time = 5;
promise = $interval(function() {
$scope.time = $scope.time - 1;
if ($scope.time === 0) {
$location.url('/news/' + $routeParams.id)
}
}, 1000, $scope.time);
$scope.$on('$destroy', function() {
$interval.cancel(promise);
});
}]);
app.run(function($rootScope, $location, $routeParams) {
$rootScope.$on('$locationChangeStart', function(evt, to, from) {
var toRegExp = '/news/' + $routeParams.id;
var fromRegExp = '/news/' + $routeParams.id + '/sponsor';
var toMatch = toRegExp.exec(to);
var fromMatch = fromRegExp.exec(from);
if (toMatch && (!fromMatch || toMatch[1] !== fromMatch[1])) {
evt.preventDefault();
$location.path('/news/' + $routeParams.id + '/sponsor').replace();
}
});
});