简体   繁体   English

在console.log中显示ngResource对象

[英]Display ngResource object in console.log

I have an app that is querying the pokeapi . 我有一个查询pokeapi的应用程序。

I'm using AngularJS ngResource in a factory to hit an endpoint and in my controller it's querying the data and the view is populating. 我在工厂中使用AngularJS ngResource命中一个端点,并且在我的控制器中它正在查询数据,并且正在填充视图。 The one thing I'm not able to do is log the actual object being returned so I can parse through it in the console. 我无法做的一件事是记录返回的实际对象,以便我可以在控制台中对其进行解析。

Controller 控制者

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

Factory

app.factory('searchCharacter', function($resource) {
  return $resource('http://pokeapi.co/api/v1/pokemon/:value', {
    value: '@searchCharacter'
  }, {
    'query': {
      method: 'GET',
      isArray: true
    }
  });
});

The only thing that I can get back in the console is: >character retrieved [object Object] 我在控制台中唯一可以得到的是: >character retrieved [object Object]

How can I get the object tree in the console? 如何在控制台中获取对象树? Image of what I'm talking about 图片什么我谈论的

Revised Controller 修改后的控制器

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

};
});

your console log isn't returning the object because you are parsing it as a string with the + 您的控制台日志未返回对象,因为您使用+将该对象解析为字符串

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

If you want to return an object, you need to separate with a comma 如果要返回对象,则需要用逗号分隔

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

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

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