Vishakha Nehe
Vishakha Nehe

Reputation: 37

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 in which one value is selected by default?

Upvotes: 1

Views: 91

Answers (2)

Daniel Krawieczyński
Daniel Krawieczyński

Reputation: 404

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>

EDIT 1: If you want to select one of the option based on some condition you can use ngSelected directive:

ng-selected="<your condition>"

in your example it will be something like that (if I understand correctly your need)

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

If that's not what your looking you have to be more specific in your questions.

Upvotes: 1

Kobi Cohen
Kobi Cohen

Reputation: 678

Try something like this:

<select ng-model="selectedid" ng-options="object as object.id in object for editIdOptionsData" class="form-control"> </select>

Upvotes: 0

Related Questions