AngularJS路径控制器不重装 [英] AngularJS Route Controller Not Reloading

查看:194
本文介绍了AngularJS路径控制器不重装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序AngularJS在它有两条路线:

I have a very simple AngularJS app that has two routes in it:

#/search
#/results

当我从一个路线导航为我预料其他一切正常。任何所需的资源被取出,并完美显示的内容。

When I navigate from one route to another everything works as I'd expected. Any resources that are needed are fetched and the content is displayed perfectly.

问题是,当我从一个路线导航到相同的路线(即#/结果#/结果)绝对没有任何反应。我从浏览器的角度来看,什么都没有发生理解,但我真的很喜欢AngularJS重新加载的内容。

The problem is when I navigate from one route to the same route (I.e., #/results to #/results) absolutely nothing happens. I understand from the browser's perspective nothing has happened but I'd really like AngularJS to reload the content.

这一定很容易,但我画这个空白。有人可以请一些线索这对我?

This must be very easy but I'm drawing blanks on this. Can someone please shed some light on this for me?

谢谢,

乔恩

推荐答案

当您导航到同一个页面,浏览器忽略它。结果
Angular.js无关与&LT的默认行为; A HREF =方式>

When you navigate to the same page, the browser ignores it.
Angular.js has nothing to do with the default behavior of <a href="">.

你能做什么,是创建一个自定义指令其中:

What you can do, is to create a custom directive which:


  • 使用 $ route.reload()重新加载当前视图(路线)。

  • 使用 $ location.path(的新路径)导航到其他视图。

  • use $route.reload() to reload the current view (route).
  • use $location.path(newPath) for navigating to other views.

我做了例如

app.directive('xref',function($route, $location){
  return {
    link: function(scope, elm,attr){
      elm.on('click',function(){
        if ( $location.path() === attr.xref ) {
          $route.reload();
        } else {
          scope.$apply(function(){
            $location.path(attr.xref);
          });
        }      
      });
    }
  };
});

在你看来只是创建:

<a xref="/">Home</a>
<a xref="/links">Links</a>

这篇关于AngularJS路径控制器不重装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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