Angular won't display object

Frenck

So i'm busy with a webapplication and i'm stuck now with parsing an object. What I just tried to do is getting the ID of all results but it gives me undefined back in my console.

What did i try:

    var app = angular.module("DB", []);

    app.controller("Controller", function($scope, $http) {
      $http.defaults.headers.common["Accept"] = "application/json";
        $http.get('api_url').

        success(function(data, status, headers, config) {
          $scope.games = data.results;
          $scope.id = data.results.id; 
         //Do i need a foreach here because it doesn't loop through all records and it gives me undefined.

        $http.get('http:/api/id/' + $scope.id + '?api_key=', function(e){ 

        }).
          success(function(data, status, headers, config) {
            $scope.details = data;
            console.log(data);
            //this returns the complete JSON
          });
        }).
        error(function(data, status, headers, config) {
          //handle errors
      });
    });

the first http.get loops through a JSON like:

"results": [
    {
    "easy": false,
    "id": 1,
    "title": "title",
    },
    {
    "easy": false,
    "id": 2,
    "title": "title",
    },
    {
    "easy": true,
    "id": 2,
    "title": "title",
    }
]

and what the second on needs to do is get all of the ID's from the JSON and start a new GET:

$http.get('http:/api/id/' + $scope.id + '?api_key=', function(e){ 

}).
  success(function(data, status, headers, config) {
    $scope.details = data;
    console.log(data.data);
  });
}) 

the problem is that is, is that $scope.id = data.results.id; is returing nothing, do i need a foreach or something to loop through it all?

And displaying it i did try:

 <div ng-repeat="detail in details">
    {{ detail }}
    {{ detail.adult }}
</div>

but is does not display anything (fyi i changed $scope.id = data.results.id; to $scope.id = data.results[0].id; to test it)

the JSON for the second GET looks like:

{
  "adult": false,
  "collection": {
    "id": 131295,
    "name": "Collection",
  },
  "levels": [{
    "id": 28
  }, {
    "id": 12
  }, {
    "id": 878
  }],
  "homepage": "google",
  "id": 100402,
  "overview": "lorem ipsum"
}

i can't access the object with {{ detail.adult }} for example.

Josep

The reason why data.results.id is returning nothing is because data.results is an Array of objects. If what you want is an Array with the IDs of the objects of data.results you could do it like this:

var resultIDs = data.results.map(function(result){return result.id;});

I'm not sure, but I think that this is what you want:

var app = angular.module("DB", []);

app.controller("Controller", function($scope, $http) {
  $scope.details = [];
  $http.defaults.headers.common["Accept"] = "application/json";
  $http.get('api_url').
  success(function(data, status, headers, config) {
      $scope.games = data.results;
      for(var i =0;i<$scope.games.lenght;i++){
         $http.get('http:/api/id/' + $scope.games[i].id + '?api_key=', function(e){ 

          }).
          success(function(data, status, headers, config) {
              $scope.details.push(data);
          });
       }
    }).
    error(function(data, status, headers, config) {
      //handle errors
  });
});

However I would check if the API that you are using has a method that accepts an array of IDs instead of a single ID, so that you don't have to iterate like that.

If this API is a RESTful service you could do it without iterating, like this:

var app = angular.module("DB", []);

app.controller("Controller", function($scope, $http) {
  $scope.details = [];
  $http.defaults.headers.common["Accept"] = "application/json";
  $http.get('api_url').
  success(function(data, status, headers, config) {
      $scope.games = data.results;
      var resultIDs = data.results.map(function(result){return result.id;});
       $http.get('http:/api?id=' + resultIDs.join(',') + '?api_key=', function(e){ 

       }).
       success(function(data, status, headers, config) {
            $scope.details = data;
       });
    }).
    error(function(data, status, headers, config) {
      //handle errors
  });
});

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Images won't display horizontally

분류에서Dev

App won't display and crashes in fullscreen

분류에서Dev

Div background won't display with nested children

분류에서Dev

List View Fragment Won't Display

분류에서Dev

Why won't the terminal display new lines?

분류에서Dev

PhpStorm won't recognize the methods of my object

분류에서Dev

Data won't load in Angular view

분류에서Dev

Python won't write small object to file but will with large object

분류에서Dev

Ubuntu 13.10 Unity Sound Indicator - Won't display changes

분류에서Dev

After being scaled an image won't display without an unrelated imageIcon

분류에서Dev

Late 2015 iMac won't wake from sleep - black display

분류에서Dev

Skewed border won't display properly in FiFO on MacOs

분류에서Dev

Why won't ls display output in multiple columns, for some directories?

분류에서Dev

Why won't this code display Error: Could not find file when e is thrown by a nonexistant file?

분류에서Dev

DTD won't verify

분류에서Dev

String won't print

분류에서Dev

RequestContext won't work

분류에서Dev

Gimp won't launch

분류에서Dev

wifi list won't show and even ethernet won't show

분류에서Dev

KDE Won't Fully Remove

분류에서Dev

kubuntu 12.04 won't start

분류에서Dev

XAMPP won't start apache

분류에서Dev

AJAXify won't work on phpBB?

분류에서Dev

MySQL Trigger won't Save

분류에서Dev

MySQL Trigger won't Save

분류에서Dev

JTextField won't disappear completely

분류에서Dev

sed won't replace text

분류에서Dev

iscsi won't start at boot

분류에서Dev

CURL won't connect to self