angularjs喷射器:location.path的unpr错误

时间:2018-11-30 14:52:02

标签: html angularjs angular-routing

我正在使用$ location.path,但是我已经注入了$ location,但我仍然无法弄清楚是什么原因导致注入器未知的提供程序错误。

(function () {
    'use strict';

    var app = angular.module('myApp');
    app.controller("headerController", headerController);

    headerController.$inject = ['$scope','$location', '$http', '$rootScope', '$q'];

    function headerController($scope, $location, $http, $rootScope, $q) {
        var vm = this;

        $scope.search = function () {
            $location.path('/searchIndex').search({ query: $scope.query }); //Error At this line
        }
    }
})();

错误:

angular.js:15018 Error: [$injector:unpr] http://errors.angularjs.org/1.7.2/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams%20%3C-%20searchResultsController
    at angular.js:99
    at angular.js:4891
    at Object.d [as get] (angular.js:5051)
    at angular.js:4896
    at d (angular.js:5051)
    at e (angular.js:5076)
    at Object.instantiate (angular.js:5120)
    at angular.js:11175
    at Object.<anonymous> (angular-ui-router.min.js:7)
    at angular.js:1364 "<div class="well ng-scope" ui-view="">"

app.js

(function () {

    var myapp = angular.module('myApp', ["ui.router"]);

    myapp.config(function ($stateProvider, $locationProvider, $urlRouterProvider) {

        // For any unmatched url, send to /route1
        $urlRouterProvider.otherwise("/homeIndex")


                $stateProvider.state('searchIndex', {
                    url: "/searchIndex",
                    templateUrl: "SOC/Views/Search/index.html",
                    controller: "searchResultsController"
                })

        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });
    });


})();

此angularjs应用程序的SearchResultsController

(function () {
    'use strict';

    var app = angular.module('myApp');

    app.controller('searchResultsController', ['$scope', '$location', '$http', '$rootScope', '$q', '$routeParams',
      function ($scope, $location, $http, $rootScope, $q, $routeParams) {
          var vm = this;

          $scope.query = $routeParams.query;
          $scope.url = $location.absUrl();


          $scope.results = [];

          $http.get($scope.url).then(function (result) { if (result.data.contains($scope.query)) $scope.results.push("url1") });

      }
    ]);
})();

请建议我是否丢失任何东西,即使我正确拼写了所有注射内容

1 个答案:

答案 0 :(得分:0)

(自从我使用ui-router已经有一段时间了,但是我认为我看到了问题)

似乎您正在使用ui路由器和标准角度路由器的组合。

执行网址更改时,应使用$state.go(...)而不是$location.path(...)。此外,在配置中注册状态时,应执行$stateProvider.state(...)而不是{{1} }(不确定是否会有所作为,但这就是Wiki在其示例中的作用。

app.js:

$urlRouterProvider.state(...)

控制器:

// For any unmatched url, send to /homeIndex
$urlRouterProvider.otherwise("/homeIndex");

$stateProvider
        .state('searchIndex', {
            url: "/searchIndex",
            templateUrl: "SOC/Views/Search/index.html",
            controller: "searchResultsController"
        });

请参阅: