问题
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