我的应用程序中有以下控制器:
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出了问题,但在更改所有后端代码之前,我想知道是否有人可以帮我解决这个问题。
祝你好运, Antonio Belloni
答案 0 :(得分:5)
contatosClientes是一个数组,你也必须为此做重复,例如:
<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>