I'm using routeProvider to set controllers and templates for my URLs. However, I've noticed that when I click on a link with the same URL as the current location, nothing happens. I would like to trigger the reload()
method even if the location hasn't changed. Essentially, I want it to behave as if the location is different every time I set it to the same value.
Is there a way to configure routeProvider or locationProvider to handle this automatically? Or what would be the best approach to achieve this in AngularJS? This behavior is common in traditional web applications, but how can it be implemented in AngularJS?
I also posted this question on Google Groups.
UPDATE:
Since this question is gaining a lot of views, I wanted to share how I resolved the issue. Based on Renan Tomal Fernandes's suggestion in the comments, I created a custom directive for handling links in my application.
angular.module('core.directives').directive('diHref', ['$location', '$route',
function($location, $route) {
return function(scope, element, attrs) {
scope.$watch('diHref', function() {
if(attrs.diHref) {
element.attr('href', attrs.diHref);
element.bind('click', function(event) {
scope.$apply(function(){
if($location.path() == attrs.diHref) $route.reload();
});
});
}
});
}
}]);
This directive can be used for all links in the application where this functionality is required.
<a di-href="/home/">Home</a>
With this directive, the href
attribute is set based on the di-href
attribute so that Angular can handle it accordingly. Additionally, when a user clicks on a link and the path matches the current path, it reloads the route.