Vishakha Nehe Vishakha Nehe - 5 months ago 15
AngularJS Question

How to use ng-repeat to show all the values coming from the API in which I need to show only one key value from all the objects?

Through my API I am having two or more objects. In objects there are different. Different key values like

id
,
name
,
account
etc. But I need to show only ids of all the objects, in select box.

I am using
ng-repeat
like this in my HTML:

<select ng-model="selectedid" class="form-control">
<option ng-repeat="id in editIdOptionsData" value="{{id}}">{{id}}</option>
</select>


And for the response I am doing :

deal.getIdData($scope.accountDetail.accountUsers[0].role, $scope.accountDetail.id)
.then(function successCb(data){
$scope.editIdOptionsData=data;
});


But like this in my select box I am getting all the objects. Can you tell me how to fetch only
id
from all the object?

Answer

What I think you're doing is setting whole object into option

<select ng-model="selectedid" class="form-control"> 
  <option ng-repeat="id in editIdOptionsData" value="{{id}}">{{id}}</option> 
</select>

the 'id' in your code is one whole object. What you should do is:

<option ng-repeat="id in editIdOptionsData" value="{{id.id}}">{{id.id}}</option>

Or just for readability

<option ng-repeat="element in editIdOptionsData" value="{{element.id}}">{{element.id}}</option>
Comments