0

I feel like I'm missing something obvious here, but I can't render the JSON data to the page, only able to out put the JSON formatted data, and not just specific keys (e.g., Name: Abbey Malt).

This is my controller:

app.controller('beerController', function($scope, $http, $modal) {
$http.get("http://api.brewerydb.com/v2/fermentables", {
    params: {
      key: 'xxx'
    }
  })
  .then(function(response) {
    $scope.response = response.data.name;
    console.log(response);
  });
};

And the HTML:

    <div ng-controller="beerController">
    <div class="container">
      <h3>Latest Releases ({{currentPage}} pages)</h3>
      <div class="row">
        <div ng-repeat="items in response">
          <li>{{data.name}}</li>
        </div>
      </div>
    </div>
</div>

Lastly, here's a screenshot of the console and how the JSON object is structured: enter image description here

2 Answers 2

5

put that on your controller:

$scope.items = response.data.data;

and html would be like this

<div ng-repeat="item in items">
    <li>{{item.name}}</li>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

@user3438917 is it clear to you why this is the correct answer? It looks like the API returns an object data that is an array. Angular itself will deserialize the JSON as resonse.data. So it becomes response.data.data. Then you are iterating over the items you have exposed in your controller. You are referencing each item as item. You can then access the name as item.name.
1

Try to change your code to

app.controller('beerController', function($scope, $http, $modal) {
    $http.get("http://api.brewerydb.com/v2/fermentables", {
        params: {
          key: 'xxx'
        }
      })
      .then(function(response) {
        $scope.response = response.data.data;
        console.log(response);
      });
    };

and view

<div>
    <div ng-controller="beerController">
    <div class="container">
      <h3>Latest Releases ({{currentPage}} pages)</h3>
      <div class="row">
        <div ng-repeat="item in response">
          <li>{{item.name}}</li>
        </div>
      </div>
    </div>
</div>

1 Comment

I was so close! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.