11

I want to AngularJS Change Path Without Reloading, look http://joelsaupe.com/programming/angularjs-change-path-without-reloading/

in core.js:

 'use strict';
    angular.module('App',['ngRoute'])
        .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]);
        };
    }]);

In controller:

    angular.module('App')        
        .controller('DetailController', ['$scope', '$location',  function($scope) {
  $scope.changeURL = function(){
            console.log("IN changeURL");
            $location.path('/sample/gfshdfdsf', false);
        };      
    }]);

If invoke changeURL, it will occur error:ReferenceError: $location is not defined

Can somebody help me? Thanks!

UNSTABLE
  • 393
  • 1
  • 4
  • 13

2 Answers2

32

$location is not injected in the controller, so just change

.controller('DetailController', ['$scope', '$location',  function($scope)

to

.controller('DetailController', ['$scope', '$location',  function($scope, $location)
sol4me
  • 15,233
  • 5
  • 34
  • 34
  • Thanks you. I changed, but new error: TypeError: $location.path is not a function. – UNSTABLE May 03 '15 at 12:21
  • change `$location.path('/sample/gfshdfdsf', false);` to `$location.path('/sample/gfshdfdsf');` – sol4me May 03 '15 at 12:24
  • sorry, $location.path = function (path, reload) is defined in .run – UNSTABLE May 03 '15 at 12:26
  • I must call $location.path('/sample/gfshdfdsf', false) to change url without reload. It define in core.js. But occur error : TypeError: $location.path is not a function. – UNSTABLE May 03 '15 at 12:35
  • Can you try `$location.path('/sample/gfshdfdsf').replace().reload(false)` – sol4me May 03 '15 at 12:47
  • Can you see this http://joelsaupe.com/programming/angularjs-change-path-without-reloading/ ? I copy the code from it. I don't know why it occur error. – UNSTABLE May 03 '15 at 12:48
  • I will be glad to have a look on it but could you please post it in a new question , since I already provide you an answer to the original question. Its better to keep the post clean for future users. Thanks – sol4me May 03 '15 at 14:42
  • Thank you , i post it in [link](http://stackoverflow.com/questions/30014504/angularjs-how-to-call-function-in-service) with some changes. Looking forward to your answer! – UNSTABLE May 03 '15 at 14:52
1

I was getting the same error and I deleted the $rootScope from the definitions. After that it worked. No idea why.

Not working

app.factory("OrganizationService",   
    ['$q', '$http', '$log', '$location', '$rootScope', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService',
  function($scope , $http, $log, $location, $cookies, $rootScope, $timeout) {

Working

app.factory("OrganizationService",   
    ['$q', '$http', '$log', '$location', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService',
  function($scope , $http, $log, $location, $cookies, $timeout) {
Andreas Panagiotidis
  • 2,763
  • 35
  • 32