Thursday, March 21, 2019

javascript - angularjs - refresh when clicked on link with actual url



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.



Home


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.



Answer



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


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...