El Hombre Sin Nombre El Hombre Sin Nombre - 1 month ago 14
Javascript Question

Ng-repeat through different objets

The question is simple. I´m trying do ng-repeat with two different objects. In this case I want to return "Name:Leanne Graham, Name: Asier". The JSON file has this.

users: {
0: {
email: "Sincere@april.biz",
id: 1,
name: "Leanne Graham",
phone: "1-770-736-8031 x56442",
username: "Bret",
website: "hildegard.org"
},
1: {
name: "Asier"
}
};


The element who has two objects is $scope.user and I try this

<div ng-repeat="(key, value) in user">{{name}}</div>


Nothing happens, and no errors in console.log. ¿Some help?

Answer

Provided your data is actually in a valid format, repeating over the collection seems simple enough...

// note: this is just an example to put your data in scope
angular.module('so', []).run(function($rootScope) {
  $rootScope.user = {
    0: {
      email: "Sincere@april.biz",
      id: 1,
      name: "Leanne Graham",
      phone: "1-770-736-8031 x56442",
      username: "Bret",
      website: "hildegard.org"
    },
    1: {
      name: "Asier"
    }
  };
});
<div ng-app="so">
  <div ng-repeat="usr in user">{{usr.name}}</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>