Angularjs中$location的Url重定向问题-无法读取property 'path'的定义

Issue with Url redirect on $location in Angularjs - Cannot read property 'path' of undefined

本文关键字:读取 property 定义 path location Url 重定向 Angularjs 问题      更新时间:2023-09-26

我是AngularJS的新手,我试图运行这个AngularJS,当我点击提交按钮时,应该修改URL而不重新加载页面,但在控制台上,我得到TypeError:无法读取属性'path'的undefined

真的看不出我错过了$location注入。有什么问题吗?

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

DI数组依赖顺序错误,DI数组第三个参数缺少$rootScope。确保注入的依赖项在控制器函数

中以相同的顺序使用。
//                                                  VVVVVVVV $rootscope was missing
app.controller('GetController', ['$http', '$scope', '$rootScope','$location',
  function($http, $scope, $rootScope, $location) {