0

Im trying to change the url with angular without reloading the page.

I came across this question and specifically this answer:

If you need to change the path, add this after your .config in your app file. Then you can do $location.path('/sampleurl', false); to prevent 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]);
    };
}])

However when i run that code i get an error straight away and console reads.

Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.6.2/$injector/unpr?p0=%24routeProvider%20%3C-%20%24route

When i click the link i read that maybe the error is because you cant pass $rootScope to anything but a directive or controller? Can someone help me explain whats going on?

Community
  • 1
  • 1
  • Have you installed ngRoute? Also show your `config()` – itsme May 07 '17 at 09:20
  • Read [AngularJS Developer Guide - Using $location](https://docs.angularjs.org/guide/$location). – georgeawg May 07 '17 at 09:42
  • @sbaaaang omg duh! thanks man, post that as a answer, the original questions answer never said it was using ngRoute and i didnt notice from the $route service.. thanks again –  May 07 '17 at 11:33

1 Answers1

0

You did not installed ngRoute :)

itsme
  • 48,972
  • 96
  • 224
  • 345