0

I'm having a AngularJS spa and want to fetch files from a JSON, which works good so far.

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

app.controller("TodoCtrl", function($scope, $http) {
  $http.get('todos.json').
    success(function(data, status, headers, config) {
      $scope.posts = data;
    }).
  error(function(data, status, headers, config) {
    alert("Error");
  });
});

and bind it to repeat a list.

<ul ng-repeat="post in posts">
  <li>
    <b>Name : </b> {{post.name}} <br>
    <b>Adress/street :</b> {{post.address.street}}
  </li>
</ul>

my problem ist, what if I have nested another object inside the JSON:

"adress": [
  {
    "street": "somewhat street",
    "town": "somewhat town"
  }]

My attempt post.adress.street does not work. Any suggestions?

1
  • You need to use another ng-repeat for Address object. Refer here for more info. Commented Sep 30, 2014 at 10:42

1 Answer 1

2

Since address is an array, you have 2 options.

First option:

Use the array notation to access only one of the items in the array. For example:

<ul ng-repeat="post in posts">
  <li>
    <b>Name : </b> {{post.name}} <br>
    <b>Adress/street :</b> {{post.address[0].street}}
  </li>
</ul> 

Second option:

Iterate over all the address items using another ng-repeat:

<ul ng-repeat="post in posts">
  <li>
    <b>Name : </b> {{post.name}} <br>
    <div ng-repeat="address in post.address">
      <b>Adress/street :</b> {{address.street}}
    </div>
  </li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

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.