Angular JS Unknown Provider Error

2020-07-20 04:27发布

I am getting this error in my angular js app and can't figure out what's causing the problem. It seems to be a common issue but all my troubleshooting hasn't helped at all. If anyone can point to waht the problem may be it would be appreciated. Thanks :)

Error: [$injector:unpr] Unknown provider: ResultsServiceProvider <- ResultsService <- ResultsController

Here is my code:

app.js

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

Controller

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

Service:

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

Factory

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

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

1条回答
smile是对你的礼貌
2楼-- · 2020-07-20 04:56

If you have an error such as this:

Error: [$injector:unpr] Unknown provider: ResultsServiceProvider <- ResultsService <- ResultsController

It usually mean one of those things:

  • You've either misspelled the ResultsService name when creating (declaring) it.
  • Or you haven't inserted a script tag pointing to the file that contains the service in your index.html.
  • Or you've created the service within a certain module other than your main app module, but have forgotten to list this module as one of your app's dependencies (e.g. angular.module('myApp', ['moduleWithService']);)

So during debugging of this kind of error, you should always start from checking these 3 things.

查看更多
登录 后发表回答