Path change without reloading the route + allow back button to reload the route (AngularJS)

六眼飞鱼酱① 提交于 2020-01-04 13:49:03

问题


So I am trying to be able to change the $location.path of my app without reloading the controllers etc. I found a workaround on this page:

https://github.com/angular/angular.js/issues/1699

The problem is that the workaround works too well and the page does not reload when pressing the back button in the browser. Another weird issue I noticed is that when I place $rootScope.$on('$locationChangeSuccess', function (){ ... }); in my app.run, it is being called over 3000 times on page load/route change...

The code that I have been using looks like this:

myService.skipReload = function () {

  var lastRoute = $route.current;

  $rootScope.$on('$locationChangeSuccess', function () {
    $route.current = lastRoute;
  });

  return MyService;
};

Then I use it like this: myService.skipReload.changePath(url); where changePath(url) simply contains $location.path(url).

Edit: I found an ugly workaround, in app.run I wrote:

$rootScope.$on('$locationChangeSuccess', function() {

  $rootScope.actualLocation = $location.path();
});


$rootScope.$watch(function () {return $location.path()}, function (newLocation, oldLocation) {
  if($rootScope.actualLocation === newLocation) {
    myLocationService.refreshView();
    console.log('Use of history back, may not always work...');
  }
});

Let me know if there is a better way to do this.

来源:https://stackoverflow.com/questions/22940106/path-change-without-reloading-the-route-allow-back-button-to-reload-the-route

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!