angularJs ng-repeat 不显示工厂数组中的所有数据
angularJs ng-repeat doesn't show all data from factory array
我对 ng-repeat 有疑问。
我有 2 个 table,其中一个要显示数据。您可以单击每个元素,然后
第二个 table 显示有关所选行的更多信息。
第二个 table 的 ng-repeat 只循环一次,即使我有 2 个对象要显示。
这是测试代码:
<!DOCTYPE html>
<html>
<head>
<title>ng-repeat doesn't work</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<script>
appTest=angular.module('testApp',[])
.factory('adminClientService',function()
{
var cliente=[];
var addCliente=function(newClient)
{
cliente.pop(); //delete element of array
cliente.push(newClient); //add element on array
};
var getCliente=function(){
return cliente;
};
var clearClientList=function()
{
cliente=[];
};
return{ addCliente:addCliente,
getCliente:getCliente,
clearClientList:clearClientList};
})
.controller('adminShowUserDataPanel',['$scope','adminClientService',function($scope,adminClientService){
this.cliente=adminClientService.getCliente();
}])
.controller('adminUserPanel',['$scope','adminClientService',function($scope,adminClientService)
{
this.clientList=[
{
name:'a',
value:1,
otherVal:[{subName:'aa'}]
},
{
name:'b',
value:2,
otherVal:[{subName:'bb'},{subName:'bbBB'}]
},
{
name:'c',
value:3,
otherVal:[{subName:'cc'},{subName:'cCcC'}]
},
];
this.selectedClient=function(data)
{
adminClientService.addCliente(data.otherVal);
};
}]);
</script>
<div ng-app="testApp">
<div id='table1'>
<table class="table table-stripped table-hover" ng-controller="adminUserPanel as panel">
<thead>
<tr><th class="text-center">#1</th><th class="text-center">#2</th></tr>
</thead>
<tbody>
<tr ng-repeat="data in panel.clientList" ng-click="panel.selectedClient(data);">
<td class="text-center">{{data.name}}</td>
<td class="text-center">{{data.value}}</td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<div id='table2'>
<table class="table table-stripped table-hover" ng-controller='adminShowUserDataPanel as showSubData'>
<thead>
<tr><th class="text-center">object have...</th><th class="text-center">data</th><th class="text-center">Len.</th></tr>
</thead>
<tbody>
<tr ng-repeat="client in showSubData.cliente">
<td class="text-center">{{client}}</td>
<td class="text-center">{{client[$index].subName}}</td>
<td class="text-center">long:{{client.length}}</td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
</body>
我读过 $apply 等,但它没有用(也许我没有正确使用它)
我正在学习 Angular 我有点困惑,第一个 table 它有效但第二个无效 =S
您好,您的数组客户端始终是一个只有一个元素的数组,请尝试下面的代码
<tbody>
<tr ng-repeat="client in showSubData.cliente[0]">
<td class="text-center">{{client.subName}}</td>
<td class="text-center">long:{{showSubData.cliente[0].length}}</td>
</tr>
</tbody>
我对 ng-repeat 有疑问。
我有 2 个 table,其中一个要显示数据。您可以单击每个元素,然后 第二个 table 显示有关所选行的更多信息。
第二个 table 的 ng-repeat 只循环一次,即使我有 2 个对象要显示。
这是测试代码:
<!DOCTYPE html>
<html>
<head>
<title>ng-repeat doesn't work</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<script>
appTest=angular.module('testApp',[])
.factory('adminClientService',function()
{
var cliente=[];
var addCliente=function(newClient)
{
cliente.pop(); //delete element of array
cliente.push(newClient); //add element on array
};
var getCliente=function(){
return cliente;
};
var clearClientList=function()
{
cliente=[];
};
return{ addCliente:addCliente,
getCliente:getCliente,
clearClientList:clearClientList};
})
.controller('adminShowUserDataPanel',['$scope','adminClientService',function($scope,adminClientService){
this.cliente=adminClientService.getCliente();
}])
.controller('adminUserPanel',['$scope','adminClientService',function($scope,adminClientService)
{
this.clientList=[
{
name:'a',
value:1,
otherVal:[{subName:'aa'}]
},
{
name:'b',
value:2,
otherVal:[{subName:'bb'},{subName:'bbBB'}]
},
{
name:'c',
value:3,
otherVal:[{subName:'cc'},{subName:'cCcC'}]
},
];
this.selectedClient=function(data)
{
adminClientService.addCliente(data.otherVal);
};
}]);
</script>
<div ng-app="testApp">
<div id='table1'>
<table class="table table-stripped table-hover" ng-controller="adminUserPanel as panel">
<thead>
<tr><th class="text-center">#1</th><th class="text-center">#2</th></tr>
</thead>
<tbody>
<tr ng-repeat="data in panel.clientList" ng-click="panel.selectedClient(data);">
<td class="text-center">{{data.name}}</td>
<td class="text-center">{{data.value}}</td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<div id='table2'>
<table class="table table-stripped table-hover" ng-controller='adminShowUserDataPanel as showSubData'>
<thead>
<tr><th class="text-center">object have...</th><th class="text-center">data</th><th class="text-center">Len.</th></tr>
</thead>
<tbody>
<tr ng-repeat="client in showSubData.cliente">
<td class="text-center">{{client}}</td>
<td class="text-center">{{client[$index].subName}}</td>
<td class="text-center">long:{{client.length}}</td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
</body>
我读过 $apply 等,但它没有用(也许我没有正确使用它)
我正在学习 Angular 我有点困惑,第一个 table 它有效但第二个无效 =S
您好,您的数组客户端始终是一个只有一个元素的数组,请尝试下面的代码
<tbody>
<tr ng-repeat="client in showSubData.cliente[0]">
<td class="text-center">{{client.subName}}</td>
<td class="text-center">long:{{showSubData.cliente[0].length}}</td>
</tr>
</tbody>