angular js - Change path but don't reload controller

196 Views Asked by At

I've tried the solution here: https://www.consolelog.io/angularjs-change-path-without-reloading/

app.run(['$route', '$rootScope', '$location', function ($route, $rootScope, $location) {
    var original = $location.path;
    $location.path = function (path, reload) {
        if (reload === false) {
            var lastRoute = $route.current;
            var un = $rootScope.$on('$locationChangeSuccess', function () {
                $route.current = lastRoute;
                un();
            });
        }
        return original.apply($location, [path]);
    };
}])

But the path I set it to just gets consumed and reverts back to how it was.

1

There are 1 best solutions below

0
georgeawg On

The default behavior of the ngRoute router is to reload the ngView when the path changes and the new URL maps to the current route.

If the reloadOnSearch option is set to false, ngRoute will not reload the route when only $location.search() or $location.hash() changes:

app.config(function($routeProvider) {
    $routeProvider.when("/foo", {
        templateUrl: 'foo.html',
        controller: 'fooController',
        reloadOnSearch: false
    });
})

And the app invokes:

$location.hash({pageNumber: n});

A $routeUpdate event is broadcast on the root scope (without reloading the route).1