简体   繁体   English

AngularJs:多次单击同一按钮不起作用

[英]AngularJs : Multiple clicks on same button is not working

I have created a web page in Angular Js. 我已经在Angular Js中创建了一个网页。 Whenever I navigate to some other page from Home page and click on Home button from that page it navigates back to Home page which is expected and working fine. 每当我从Home导航到其他页面并单击该页面上的“ Home按钮时,它就会导航回到期望的Home ,并且工作正常。 But when again I click on Home button then I'm expecting a page refresh (because currently it is Home page) but it is not happening. 但是当我再次单击“ Home按钮时,我期望页面刷新(因为当前是“ Home ),但是没有发生。

Angular does not reload the view when the $location of the route you are trying to go to is the same as the current route you are on. 当您要转到的路线的$location与您当前所在的路线相同时,Angular不会重新加载视图。 You can use the reload() method to achieve this. 您可以使用reload()方法来实现。 See https://docs.angularjs.org/api/ngRoute/service/$route 参见https://docs.angularjs.org/api/ngRoute/service/$route

(Make sure you switch the api version to match the Angular version you are using) (确保您切换api版本以匹配您使用的Angular版本)

For a cleaner solution (imho) than using search parameters see the following code (I am using controllerAs syntax): 对于比使用搜索参数更清洁的解决方案(imho),请参见以下代码(我正在使用controllerAs语法):

angular.module('app').controller('MainController', [
  '$location',
  '$route',
  function ($location, $route) {
    var main = this;

    main.goToHome = function () {
      if ($location.path() === '/') {
        $route.reload();
      }
    };
  }
]);

in combination with calling that function on your home button link on click: 结合点击以下按钮在您的主页按钮链接上调用该函数:

<a href="#/" ng-click="main.goToHome()">Home</a>

This function checks the location when you click your button, if the location is / then it reloads the view. 当您单击按钮时,此功能检查位置,如果位置为/则重新加载视图。

Of course you can replace the route url with whatever url you use for your home route. 当然,您可以将路由网址替换为用于家庭路由的任何网址。

The following is an untested, alternative solution I came up with. 以下是我提出的未经测试的替代解决方案。

In your routes configuration object for the home page (which I am assuming has a url of /home ), specify the following parameters: 在主页的路由配置对象(我假设其URL为/home )中,指定以下参数:

{
    // ...
    reloadOnSearch: true,
    redirectTo: function (routeParams, path, search) {
         if (search.redirected) {
             // don't redirect, so return same path + search
             return '/home?redirected=true';
         } else {
             return '/home';
    },
    // ...
}

The thinking is that when you link to /home , the redirectTo() function will fire and redirect you to /home?redirected=true . 这种想法是,当您链接到/homeredirectTo()函数将被触发并将您重定向到/home?redirected=true That will be a change to the search parameter, so the route should reload correctly due to specifying reloadOnSearch: true . 这将是对search参数的更改,因此由于指定了reloadOnSearch: true ,因此路由应该正确地重新加载。 Since the home page links will always be pointing to /home , the page should always reload. 由于主页链接将始终指向/home ,因此页面应始终重新加载。

It's a bit ugly, and will likely cause the home page controller to run twice when going from some other page back to the home page, but if you can't get any other way to work, this one might be worth a try. 这有点丑陋,当从其他页面返回到主页时,可能会导致主页控制器运行两次,但是如果您无法找到其他工作方式,则可以尝试一下。

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

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