1
我试图通过在服务中执行http请求来清理我的控制器,但出于某种原因,我的控制器中的某个函数没有从服务获取响应。控制器没有得到工厂的响应
我有我的控制器此功能,
$scope.addMovie = function() {
addMovie.add().then(function(response){
if(response){
console.log ('Response')
} else {
console.log ('No reposone')
}
});
这是服务,
(function(){
"use strict";
angular.module('addMovieseat')
.factory('addMovie',
function($http, $q){
return{
add: function(){
var deferred = $q.defer();
'http://api.themoviedb.org/3/movie/206647?api_key=a8f7039633f2065942cd8a28d7cadad4&append_to_response=releases'
// Search for release dates using the ID.
var base = 'http://api.themoviedb.org/3/movie/';
var movieID = $(event.currentTarget).parent().find('.movieID').text()
var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
var append_to_response = '&append_to_response=releases'
var callback = 'JSON_CALLBACK'; // provided by angular.js
var url = base + movieID + '?api_key=' + apiKey + append_to_response + '&callback=' + callback;
$http.jsonp(url,{ cache: true}).
success(function(data, status, headers, config) {
if (status == 200) {
deferred.resolve(data.results);
console.log ('Succes getting data')
} else {
console.error('Error happened while getting the movie list.')
}
}).
error(function (data, status, headers, config) {
console.error('Error happened while getting the movie list.')
deferred.resolve(false);
});
$(".search_results").fadeOut(250);
return deferred.promise;
}
}
})
})();
当我运行从控制器$scope.addMovie
功能控制台日志显示Succes getting data
从服务然后从控制器中的功能No response
。这里缺少什么?
//编辑。现在我想从服务中的数据插入到在我的控制器作用域,
$scope.addMovie = function() {
$scope.movieListID = {};
console.log ('empty' + $scope.movieListID)
movieAdd.add()
.then(function(response){
$scope.movieListID = response;
console.log ('Not empty' + $scope.movieListID)
})
.catch(function(response) {
console.log ('No search reposone')
});
}
但控制台日志显示afther“响应确定”一个“不确定”的消息。
谢谢,这表明答复是成功的。我试图从JSON请求中插入数据到控制器的作用域中。 '$ scope.movieListID = response; console.log($ scope.movieListID)''但是这会在控制台中返回一个“undefined”。 –
'$ scope.movieListID'未定义?在什么时候?工厂运行并成功返回数据后? – macrog
对不起,我没有发布我的新的'addMovie'功能。我已将它添加到我的帖子。我尝试从'$ scope.movieListID'中插入响应中的数据,并在console.log中找到未定义的范围。 –