Angular JS 未知提供程序错误 [英] Angular JS Unknown Provider Error

查看:22
本文介绍了Angular JS 未知提供程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 angular js 应用程序中收到此错误,但无法找出导致问题的原因.这似乎是一个常见问题,但我所有的故障排除都没有帮助.如果有人可以指出问题可能是什么,将不胜感激.谢谢:)

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 :)

错误:[$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'
      });
   }])

控制器

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

服务:

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

工厂

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

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

推荐答案

如果你有这样的错误:

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

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

通常意味着其中之一:

  • 您在创建(声明)时拼错了 ResultsService 名称.
  • 或者您没有插入指向包含 index.html 中的服务的文件的脚本标记.
  • 或者您在主应用模块以外的某个模块中创建了服务,但是忘记将此模块列为应用的依赖项之一(例如 angular.module('myApp', ['moduleWithService']);)
  • 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']);)

所以在调试这种错误时,你应该始终从检查这3件事开始.

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

这篇关于Angular JS 未知提供程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆