我使用AngularJS
和JSON
文件来保存我的数据。我的数据包含一个包含对象和一些值的数组。
这是我的JSON:
{
"DirectoryList":
[
{
"_id": 2,
"navLvl": 1,
"codeName": "BOD",
"DirectoryName": "Board of Directors",
"Contacts": [
{
"BodName": "FVC",
"Position": "CEO",
"LocalNo": "101",
"Mobile": ["09178985698", "09178985698"]
},
{
"BodName": "MIC",
"Position": "PRESIDENT",
"LocalNo": "108",
"Mobile": ["09178710088", "09178889088"]
},
{
"BodName": "SIC",
"Position": "SVC-OPTRNS",
"LocalNo": "105",
"Mobile": ["09178923689", "09328922955"]
}
]
}
]
}
在我的控制器中:
$http.get('./json/directory.json')
.success(function(data){
var result = $filter('filter')(data.DirectoryList, {_id:$stateParams.id})[0];
$scope.listviews = [];
angular.forEach(result, function(value, key) {
$scope.listviews.push(value);
});
})
.error(function(err){
$log.error(err);
})
在我看来:
<div class="table-responsive" ng-if="listviews[0] == 2">
<table class="table table-striped table-hover table-condensed table-bordered">
<tr>
<thead>
<th>BOD Name</th>
<th>Position</th>
<th>Local No.</th>
<th>Mobile</th>
</thead>
</tr>
<tr>
<tbody ng-repeat="list in listviews[4]">
<td ng-repeat="(key, value) in list">{{ value }}</td>
</tbody>
</tr>
</table>
</div>
这给了我:
BOD Name | Position |Local No. | Mobile
---------+----------+----------+-------------------------------
FVC | CEO | 101 | ["09178985698","09178985698"]
---------+----------+----------+-------------------------------
MIC | PRESIDENT| 108 | ["09178710088","09178889088"]
---------+----------+----------+-------------------------------
SIC |SVC-OPTRNS| 105 | ["09178923689","09328922955"]
我不知道如何正确显示它但是当我尝试将value
更改为value[0]
时,我可以获得第一个数组,但它会破坏对象的数据。是否有另一种方法将数据从对象与值一起获取?无法弄清楚我应该在哪里执行逻辑。在控制器或我看来它更好吗?
答案 0 :(得分:1)
我很确定有很多不同的方法可以做到这一点。以下是应该有效的方法。
<tbody ng-repeat="list in listviews[4]">
<td ng-repeat="(key, value) in list">
<div ng-if="key=='Mobile'" ng-repeat="phone in value">{{phone}} </div>
<div ng-if="key!='Mobile'">{{value}}</div>
</td>
</tbody>