Json 范围的console.log未定义
我试图通过在控制台中记录JSON输出来访问数据,但控制台说元素未定义 我有一个控制器,它有两个功能。两者都从moviedb.org数据库执行http请求 第一个函数执行与用户输入相对应的搜索查询。这工作正常,Json 范围的console.log未定义,json,angularjs,Json,Angularjs,我试图通过在控制台中记录JSON输出来访问数据,但控制台说元素未定义 我有一个控制器,它有两个功能。两者都从moviedb.org数据库执行http请求 第一个函数执行与用户输入相对应的搜索查询。这工作正常,console.log也在这里工作,我看到了返回的所有对象 .controller('searchCtrl', [ '$scope', 'movieService', 'createMovie', 'removeMovie', '$http', function($scope, mov
console.log
也在这里工作,我看到了返回的所有对象
.controller('searchCtrl', [
'$scope', 'movieService', 'createMovie', 'removeMovie', '$http', function($scope, movieService, createMovie, removeMovie, $http) {
$scope.search = function() {
var base = 'http://api.themoviedb.org/3';
var service = '/search/movie';
var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
var search = $scope.searchquery
var callback = 'JSON_CALLBACK'; // provided by angular.js
var url = base + service + '?api_key=' + apiKey + search + '&callback=' + callback;
$http.jsonp(url,{ cache: true}).
success(function (data, status, headers, config) {
if (status == 200) {
$scope.movieList = data.results;
console.log($scope.movieList)
} 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.')
});
}
然后在模板中显示搜索结果
%ul#showresults
%li.search_results{"ng-repeat" => "movie in movieList | orderBy:'-release_date'"}
.addmovie{"ng-click" => "addMovie()"}
%span
Add Movie
%img.poster{"ng-src" => "http://image.tmdb.org/t/p/w500/{{ movie.poster_path }}"}
%span.movieID
{{ movie.id }}
%span.title
{{ movie.original_title }}
%span.release
{{ movie.release_date }}
如您所见,我有一个带有addMovie()
函数的元素。当用户单击此元素时,搜索函数触发的同一控制器中的addMovie
函数
$scope.addMovie = function() {
'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) {
$scope.movieListID = data.results;
console.log('succes!' + $scope.movieListID)
} 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.')
});
};
它从用户想要添加的电影中获取ID,并创建一个确实发生的http请求。它在“网络”选项卡中显示正确的链接。但是控制台日志显示成功!未定义
。成功用于获取JSON数据,未定义的是scope.MovieListID
所以问题是,为什么这个
console.log('succes!'+$scope.movieListID)
没有定义?我在浏览器中打开了api链接。返回的JSON没有根键结果
改变
$scope.movieListID = data.results;
到
有/没有
数据。结果
那么。谢谢,这很有效,但我不知道为什么,为什么搜索查询可以处理结果,而电影查询不能?
$scope.movieListID = data;