简体   繁体   English

AngularJS路由控制器不重载

[英]AngularJS Route Controller Not Reloading

I have a very simple AngularJS app that has two routes in it: 我有一个非常简单的AngularJS应用程序,里面有两个路由:

#/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. 获取所需的任何资源并完美显示内容。

The problem is when I navigate from one route to the same route (Ie, #/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. 我从浏览器的角度理解没有发生任何事情,但我真的很喜欢AngularJS重新加载内容。

This must be very easy but I'm drawing blanks on this. 这一定非常容易,但我在这方面画空白。 Can someone please shed some light on this for me? 有人可以帮我解释一下吗?

Thanks, 谢谢,

Jon 乔恩

When you navigate to the same page, the browser ignores it. 导航到同一页面时, 浏览器会忽略它。
Angular.js has nothing to do with the default behavior of <a href=""> . Angular.js与<a href="">的默认行为无关。

What you can do, is to create a custom directive which: 你能做的是创建一个自定义指令

  • use $route.reload() to reload the current view (route). 使用$route.reload()重新加载当前视图(路由)。
  • use $location.path(newPath) for navigating to other views. 使用$location.path(newPath)导航到其他视图。

I made an example : 我做了一个例子

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

In your view just create: 在您的视图中,只需创建:

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

You could check if the current route is the same as the "destination" route and call $route 's reload() method: 您可以检查当前路由是否与“目标”路由相同,并调用$routereload()方法:

reload() 重装()
Causes $route service to reload the current route even if $location hasn't changed. 即使$ location未更改,也会导致$ route服务重新加载当前路由。
As a result of that, ngView creates new scope, reinstantiates the controller. 因此,ngView创建新范围,重新实例化控制器。


Eg: 例如:

In HTML: 在HTML中:

<a href="" ng-click="toResults()">Results</a>

In controller: 在控制器中:

// ...first have `$location` and `$route` injected...
$scope.toResults = function () {
    var dstPath = '/results';
    if ($location.path() === dstPath) {
        $route.reload()  ;
    } else {
        $location.path(dstPath);
    }
}

I used the routeparams property as well to compare the url parameters and it worked like a charm. 我也使用了routeparams属性来比较url参数,它就像魅力一样。 In case anyone faces this problem again, here's how I resolved it: 如果有人再次遇到这个问题,我的解决方法如下:

 if ($location.path() === '/destination' && $routeParams.Name == Name) {
                $route.reload();
            }
            else {
                $location.path('/destination').search({ Name: Name });
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM