繁体   English   中英

在console.log中显示ngResource对象

[英]Display ngResource object in console.log

我有一个查询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]

如何在控制台中获取对象树? 图片什么我谈论的

修改后的控制器

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);
});

};
});

您的控制台日志未返回对象,因为您使用+将该对象解析为字符串

console.log('character retrieved' + $scope.characters);

如果要返回对象,则需要用逗号分隔

console.log('character retrieved', $scope.characters);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM