Angular JS未知提供程序错误

kel*_*nia 3 angularjs angularjs-service angularjs-factory

我在我的角度js应用程序中收到此错误,无法弄清楚导致问题的原因.这似乎是一个常见的问题,但我的所有故障排除都没有帮助.如果任何人都可以指出问题可能会受到赞赏.谢谢 :)

错误:[$ injector:unpr]未知提供者:ResultsServiceProvider < - ResultsService < - ResultsController

这是我的代码:

app.js

   angular.module('resultsApp', ['ngRoute', 'nvd3', 'ngResource'])
   .config(['$routeProvider', function($routeProvider) {
       $routeProvider.when('/results', {
       controller: 'ResultsController',
        templateUrl: 'app/results/Results.html'
      });
   }])
Run Code Online (Sandbox Code Playgroud)

调节器

    angular
          .module('resultsApp')
          .controller('ResultsController', ResultsController);

   function ResultsController($scope, ResultsService) {
      $scope.teams = [{teamA: ''}, {teamB: ''}];
      $scope.premResults = [];

      $scope.searchByTeams = function () {
      ResultsService.getResultsList($scope.teams.teamA,$scope.teams.teamB,   function (res) {
      $scope.premResults = res;
    );
  };
}
Run Code Online (Sandbox Code Playgroud)

服务:

angular
.module('resultsApp')
.service('ResultsService', ResultsService);

    function ResultsService(ResultFactory) {

        this.getResultsList = getResultsList;

        function getResultsList(teamA, teamB, callback) {
            return ResultFactory.get({teamA: teamA, teamB: teamB}, callback);
    }
}
Run Code Online (Sandbox Code Playgroud)

angular
    .module('resultsApp')
    .factory('ResultFactory', ResultFactory);

function ResultFactory($resource) {
    return $resource('api/results', {}, {
        get: {method: 'get', isArray: true}
    });
 }
Run Code Online (Sandbox Code Playgroud)

Tom*_*ski 7

如果您有这样的错误:

错误:[$ injector:unpr]未知提供者:ResultsServiceProvider < - ResultsService < - ResultsController

它通常意味着其中一件事:

  • 在创建(声明)它时,你要么拼错了ResultsService名字.
  • 或者您还没有插入指向包含您的服务的文件的脚本标记index.html.
  • 或者您已经在主应用程序模块以外的某个模块中创建了服务,但忘记将此模块列为应用程序的依赖项之一(例如angular.module('myApp', ['moduleWithService']);)

因此,在调试此类错误时,您应该始终从检查这三件事开始.