angularjs - 单击带有实际 url 的链接时刷新 [英] angularjs - refresh when clicked on link with actual url

查看:23
本文介绍了angularjs - 单击带有实际 url 的链接时刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 routeProvider 为我的 url 定义控制器和模板.

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

当我单击与实际位置具有相同网址的链接时,没有任何反应.我希望 reload() 方法在用户点击这样的链接时被调用,即使位置没有改变.换句话说,如果我将位置设置为相同的值,我希望它的行为与我将其设置为不同的值一样.

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.

有没有办法配置 routeProvider 或 locationProvider 来自动执行?或者这样做的正确方法是什么?这是往返应用程序中的标准行为,但如何在 angularjs 中做到这一点?

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?

我也在 google 群组 上询问过.

更新:

这个问题获得了很多观点,所以我将尝试解释我是如何解决我的问题的.

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

我创建了一个自定义指令,用于在我的应用程序中进行链接,正如 Renan Tomal Fernandes 在评论中所建议的那样.

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>

该指令的作用是根据 di-href 属性为您设置 href 属性,因此 angular 可以像往常一样处理它,并且您可以在你将鼠标悬停在链接上.此外,当用户点击它并且链接的路径与当前路径相同时,它会重新加载路由.

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.

推荐答案

你应该使用 $route.reload() 强制重新加载.

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

我不知道是否有一种自动"方式可以做到这一点,但您可以使用 ng-click 这些链接

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

这篇关于angularjs - 单击带有实际 url 的链接时刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆