我试图将 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:12 在 http://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/