I have a template
that is displayed temporarily, the problem with this is that it is displayed only if I click on a predefined link.
What I want to do is that the temporary template is displayed when accessing the URL with id (/ news /: id) automatically.
I think this could be implemented by saving a value with localStorage
, so if I visit the URL /news/:id
the controller sees if that value is saved, if so it does not show the temporary template, and if the value "does not is saved "then if it shows.
The controller would then save the value in localStorage
if it is not and show the temporary template for 5 seconds, then when the temporary template redirects me to the URL with id that I should have accessed from the beginning that you erase that value in the localStorage when you access it again, show the temporary template.
The problem is that I do not know how to implement something like that, I hope you can help me with an example, I'm new in this programming.
<a ng-href="/news/{{item.id}}/sponsor"></a>
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();
}
});
});