angularjs - 刷新与实际的URL链接,当点击 [英] angularjs - refresh when clicked on link with actual url

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

问题描述

我用routeProvider定义controlers和模板我的网址。

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

在我的链接,其中有相同的URL点击的是实际的位置,没有任何反应。我想重装()方法被调用,如果用户点击,即使位置并没有改变这样一个链接。换句话说,如果我设置的位置值相同,我想它有同样的表现,如果我将它设置为不同的值。

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自动做到这一点的方法吗?或者有什么是做这种正确的方法?这是往返应用stadard行为,但如何做到在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?

我问它谷歌组为好。

更新:

此问题,获得了大量的意见,所以我会尽力解释我是如何解决我的问题。

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

我创建了一个自定义的指令,在我的应用程序连接的勒南TOMAL费尔南德斯意见提出。

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>

什么这个指令的作用是在于它是基于二HREF设置的href 属性为您属性让角可以处理它像往常一样,你可以看到的网址,当你将鼠标悬停在链接。此外,当用户点击它和链接的路径是相同的,因为它重新加载路由的电流通路。

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( )强制重装。

我不知道是否有一个自动的方式做到这一点,但你可以用NG-点击

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天全站免登陆