我的应用程序中的未知提供者

时间:2018-07-06 04:50:02

标签: javascript angularjs node.js

我试图将HTTP请求放入服务文件中,以便在多个控制器中使用此功能,但是出现以下错误,我尝试了多种解决方案,但仍然收到此错误

这是我的controller.js

.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/LatestMovies', {
templateUrl: 'LatestMovies/LatestMovies.html',
controller: 'LatestMoviesCtrl'

});
}])


.controller('LatestMoviesCtrl', 
['myServices','$scope',function(myServices,$scope) {



$scope.movies = myServices.getLatestMovies();
console.log($scope.movies ,'ggg')
}
]);

这是我的services.js

   angular.module('myApp', ['ngRoute'])

 .factory('myServices', function ($http) {
return {
    getLatestMovies : function () {
            var service ={
                movies : '',
                details : []
            }
            var base = 'http://api.themoviedb.org/3';
            var service = '/movie/popular?page=1&language=en- 
             US&api_key=';
            var apiKey = '';
            var callback = 'JSON_CALLBACK';
            var url =base + service + apiKey + '&callback=' + callback;
            //$scope.results = 'requesting...';

            $http.jsonp(url).then(function(result, status) {
                //$scope.results = JSON.stringify(data);
                service.movies = result.data;
                angular.forEach(result.data.results, function (value, 
                index) {
                    service.details.push(value);
                });

            },function(result, status) {
                service.movies = 'Maybe you missed your API key?' + 
       JSON.stringify(result.data);
            });

            return service.movies;
        }
    }

    });

错误

angular.js:14199错误:[$ injector:unpr]未知提供程序:myServicesProvider <-m​​yServices <-LatestMoviesCtrl http://errors.angularjs.org/1.5.11/ $ injector / unpr?p0 = myServicesProvider%20%3C-%20myServices%20%3C-%20LatestMoviesCtrl     在http://localhost:8000/bower_components/angular/angular.js:68:12     在http://localhost:8000/bower_components/angular/angular.js:4563:19     在Object.getService [获取时(http://localhost:8000/bower_components/angular/angular.js:4716:32)     在http://localhost:8000/bower_components/angular/angular.js:4568:45     在getService(http://localhost:8000/bower_components/angular/angular.js:4716:32)     在injectionArgs(http://localhost:8000/bower_components/angular/angular.js:4741:58)     在Object.instantiate(http://localhost:8000/bower_components/angular/angular.js:4783:18)     在$ controller

2 个答案:

答案 0 :(得分:1)

您需要注入$ http模块来发送api调用

 .factory('myServices', ['$http',function ($http) {
return {
    getLatestMovies : function () {
            var service ={
                movies : '',
                details : []
            }
            var base = 'http://api.themoviedb.org/3';
            var service = '/movie/popular?page=1&language=en- 
             US&api_key=';
            var apiKey = '';
            var callback = 'JSON_CALLBACK';
            var url =base + service + apiKey + '&callback=' + callback;
            //$scope.results = 'requesting...';

            $http.jsonp(url).then(function(result, status) {
                //$scope.results = JSON.stringify(data);
                service.movies = result.data;
                angular.forEach(result.data.results, function (value, 
                index) {
                    service.details.push(value);
                });

            },function(result, status) {
                service.movies = 'Maybe you missed your API key?' + 
       JSON.stringify(result.data);
            });

            return service.movies;
        }
    }

    }]);

答案 1 :(得分:0)

似乎您已经两次创建myApp模块,首先在services.js中创建,然后在controller.js中创建。所以基本上会发生

  1. 首先加载services.js时,它将使用myApp服务创建myServices角度模块。
  2. 当加载controllers.js文件时,在文件的开头,您已经使用myApp重新创建了angular.module('myApp', ['ngRoute'])模块。在其中重新创建新的myApp模块并向其中注册configLatestMoviesCtrl
  3. 因此myServices依赖项不可用。

要在注册其他文件时解决此问题。

angular.module('myApp')
.config(...)
.controller('LatestMoviesCtrl', .... )