我有一个正在查询pokeapi的应用。
我在工厂中使用AngularJS ngResource来命中端点,在我的控制器中它查询数据并填充视图。我无法做的一件事是记录返回的实际对象,以便我可以在控制台中解析它。
控制器
app.controller('pokedexCtrl', function($scope, $http, $timeout, searchCharacter) {
$scope.search = function() {
// get pokemon details
var filteredSearch = $scope.searchCharacter.toLowerCase().replace(/\s+/g, '');
$scope.characters = [searchCharacter.get({
value: $scope.searchCharacter
}, function() {
console.log('character retrieved' + $scope.characters);
},
function(response) {
if (response.status === 404) {
alert('we are sorry but we could not find what you are looking for')
}
}
)];
};
});
工厂
app.factory('searchCharacter', function($resource) {
return $resource('http://pokeapi.co/api/v1/pokemon/:value', {
value: '@searchCharacter'
}, {
'query': {
method: 'GET',
isArray: true
}
});
});
我可以在控制台中找到的唯一一件事是:
>character retrieved [object Object]
如何在控制台中获取对象树?我所谈论的Image
修订控制器
app.controller('pokedexCtrl', function($scope, searchCharacter){
$scope.search = function(){
// get pokemon details
var filteredSearch = $scope.searchCharacter.toLowerCase().replace(/\s+/g,'');
$scope.characters = [searchCharacter.get({
value : filteredSearch
}, function(){},
function(response){
if(response.status === 404){
alert('we are sorry but we could not find what you are looking for')
}
}
)];
var pokemonObject = searchCharacter.get({value: filteredSearch}, function(value, responseHeader) {
console.log('Pokemon Object: ', pokemonObject);
});
};
});
答案 0 :(得分:0)
您的控制台日志未返回该对象,因为您正在将其解析为包含+
console.log('character retrieved' + $scope.characters);
如果要返回对象,则需要用逗号分隔
console.log('character retrieved', $scope.characters);