Problems parsing nested JSON with AngularJS

I have the following contoller in my application:

angular.module('myApp.controllers', []).
  controller('ClientesController', [ '$scope', '$rootScope', 'Clientes',
     function($scope, $rootScope, Clientes) {
        $rootScope.clientes;

        Clientes.query(function(response) {
          $rootScope.clientes = response;
  })
}]);

Which returns a JSON object with the following format:

 [{"idCliente":1,
   "nomeFantasia":"Flores",
   "razaoSocial":"Transportes Flores Ltda.",
   "contatosClientes": 
   [  
      {"idContatoCliente":1,
       "dddCelular":21,
       "email":"ljames@cavaliers.com"},
      {"idContatoCliente":2,
       "dddCelular":21,
       "email":"test@cavaliers.com"}
   ]
  }]

And a template that tries to parse JSON with ng-repeat:

<tr ng-repeat="cliente in clientes | filter:searchText">
  <td>{{cliente.idCliente}}</td>
  <td>{{cliente.razaoSocial}}</td>
  <td>{{cliente.nomeFantasia}}</td>
  <td>{{cliente.contatosClientes.email}}</td>
  <td>
     <div class="right floated ui green icon buttons">
        <div class="ui button">Editar</i></div>
     </div>
  </td>
</tr>

The problem is that I can access the foreign keys (idCliente, razaoSocial, etc.) using the sintaxe of the .key object, but I cannot access the internal keys (contatosClientes.email, for example) in a nested array.

I tried many approaches, and I'm starting to think that something is wrong with my REST API, but before changing all my internal code, I would like to know if anyone can help me with this problem.

Regards, Antonio Belloni

+4
source share
1 answer

contatosClientes - , ng-repeat, :

<tr ng-repeat="cliente in clientes | filter:searchText">
  <td>{{cliente.idCliente}}</td>
  <td>{{cliente.razaoSocial}}</td>
  <td>{{cliente.nomeFantasia}}</td>
  <td ng-repeat="contato in cliente.contatosClientes>{{contato.email}}</td>
  <td>
     <div class="right floated ui green icon buttons">
        <div class="ui button">Editar</i></div>
     </div>
  </td>
</tr>
+5

All Articles