1

I'm new to AngularJS and I'm trying to run this AngularJS that should modify the URL without reloading the page when I click on my submit button but in console I get TypeError: Cannot read property 'path' of undefined.

Can't really see where I missed $location injection. What could be the problem?

var app = angular.module("SearchAPP", ['ngRoute']);

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]);
    };
  }
]);

app.controller('GetController', ['$http', '$scope', '$location',
  function($http, $scope, $rootScope, $location) {

    $scope.click = function() {

      var response = $http({
        url: 'http://localhost:4567/search',
        method: "GET",
        params: {
          keyword: $scope.searchKeyword
        }
      });

      response.success(function(data, status, headers, config) {
        $scope.searchResults1 = data;
        // $http.defaults.useXDomain = true;
        $location.path('/' + $scope.searchKeyword, false);
      });

      response.error(function(data, status, headers, config) {
        alert("Error.");
      });
    };
  }
]);

1 Answer 1

1

Wrong dependency sequence in DI array, $rootScope is missing from DI array 3rd parameter. Make sure injected dependency should be using in same sequence in controller function

//                                                  VVVVVVVV $rootscope was missing
app.controller('GetController', ['$http', '$scope', '$rootScope','$location',
  function($http, $scope, $rootScope, $location) {
Sign up to request clarification or add additional context in comments.

3 Comments

@VISHALPRAKASH glad to help you, Thanks :)
@MrJSingh Thanks for appreciating, Cheers :)
@VISHALPRAKASH please accept answer, so that it would help you :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.