【问题标题】:Issues parsing a nested JSON with AngularJS使用 AngularJS 解析嵌套 JSON 的问题
【发布时间】:2015-07-06 19:06:14
【问题描述】:

我的应用中有以下控制器:

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

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

返回具有以下格式的 JSON 对象:

 [{"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"}
   ]
  }]

还有一个模板尝试使用 ng-repeat 解析 JSON:

<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>

问题是我可以使用 object.key sintaxe 访问外部键(idCliente、razaoSocial 等),但我无法访问嵌套数组中的内部键(例如 contatosClientes.email)。

我尝试了很多方法,我开始认为我的 REST API 有问题,但在更改我所有的后端代码之前,我想知道是否有人可以帮助我解决这个问题。

最好的问候, 安东尼奥·贝洛尼

【问题讨论】:

  • contatosClientes 是一个数组,您也必须为此执行 ng-repeat

标签: html json angularjs rest


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2020-11-14
    • 2020-02-22
    • 2013-11-18
    相关资源
    最近更新 更多