Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  117] [ 2]  / answers: 1 / hits: 80950  / 12 Years ago, fri, june 29, 2012, 12:00:00

I use routeProvider to define controlers and templates for my urls.



When I click on the link, which has the same url as is the actual location, nothing happens. I would like the reload() method to be called if a user clicks on such a link even if the location hasn't changed. In other words, if I set the location to the same value, I would like it to behave the same as if I would set it to different value.



Is there a way to configure routeProvider or locationProvider to do it automatically? Or what is the right approach to do this? This is stadard behaviour in round trip applications, but how to do it in angularjs?



I've asked it on google groups as well.



UPDATE:



This question is getting lots of views, so I will try to explain how I solved my problem.



I created a custom directive for linking in my app as Renan Tomal Fernandes suggested in comments.



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();
});
});
}
});
}
}]);


The directive is then used for all links in my app I want to have this functionality.



<a di-href=/home/>Home</a>


What this directive does is that it sets the href attribute for you based on di-href attribute so angular can handle it like always and you can see the url when you hover over the link. Furthermore when user clicks on it and the link's path is the same as the current path it reloads the route.


More From » angularjs

 Answers
16

you should use $route.reload() to force the reload.



I don't know if is there a 'automatic' way to do this, but you can use ng-click on these links


[#84562] Thursday, June 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
juand

Total Points: 366
Total Questions: 95
Total Answers: 90

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
juand questions
Sat, Sep 18, 21, 00:00, 3 Years ago
Thu, Mar 4, 21, 00:00, 3 Years ago
Thu, Nov 26, 20, 00:00, 4 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;