Lekz Flores Lekz Flores - 5 months ago 7
AngularJS Question

Get objects and values inside an array at the same time

I'm using

AngularJS
and a
JSON
file to hold my data. And my data holds an array with objects and some values.

Here's my 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"]
}
]
}
]
}


And in my controller:

$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);
})


And in my views:

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


This gives me:

BOD Name | Position |Local No. | Mobile
---------+----------+----------+-------------------------------
FVC | CEO | 101 | ["09178985698","09178985698"]
---------+----------+----------+-------------------------------
MIC | PRESIDENT| 108 | ["09178710088","09178889088"]
---------+----------+----------+-------------------------------
SIC |SVC-OPTRNS| 105 | ["09178923689","09328922955"]


I have no idea how to display it correctly but when I try to change the
value
to
value[0]
I can get the first array but it ruins the data holds by objects. Is there another way getting the data from objects together with values? Can't figure out where should I execute the logic. Is it way better in the controller or in my view?

Answer

I'm pretty sure there is a lot of different ways on doing this. The following is one that should work.

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