javascript - 我的应用程序中的未知提供商

标签 javascript angularjs node.js

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

这是我的 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 <- myServices <- 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:12http://localhost:8000/bower_components/angular/angular.js:4563:19 在 Object.getService [as get] ( 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

最佳答案

你需要注入(inject)$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;
        }
    }

    }]);

关于javascript - 我的应用程序中的未知提供商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51203196/

相关文章:

包含数百万行的 JavaScript 数据网格

javascript - 在 AngularJS View 中的移动网络中显示原生广告时出现问题

javascript - 当最初在范围内设置值时,日期选择器弹出格式不起作用

javascript - 在 Angular 中绑定(bind) JSON 数据

javascript - 解析 : ParseError { code: 101, 消息: 'Object not found.' }

node.js - 如何从 Nodejs 发送包含 html 内容的 SENDGRID 电子邮件?

javascript - Highcharts - 无法更改线条颜色

Javascript history.back 不工作

javascript - 从 ZigJS 访问 Kinect RGB 图像数据

node.js - 如何配置 OpsWorks 以部署不受官方支持的 Node.js 版本?