如何使用ng-repeat显示来自API的所有值,而我只需要显示所有对象中的一个键值? [英] 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?
问题描述
通过我的API,我有两个或多个对象.在对象中有不同.不同的键值,例如 id
, name
, account
等.但是我只需要在选择框中显示所有对象的ID.>
我在HTML中像这样使用 ng-repeat
:
< select ng-model ="selectedid" class ="form-control">< option ng-repeat ="id in editIdOptionsData" value ="{{id}}"> {{id}}</option></select>
为了回应,我正在这样做:
deal.getIdData($ scope.accountDetail.accountUsers [0] .role,$ scope.accountDetail.id).then(函数successCb(data){$ scope.editIdOptionsData = data;});
但是像这样在我的选择框中,我得到了所有对象.您能告诉我如何从默认选择一个值的所有对象中仅获取 id
吗?
我认为您正在将整个对象设置为选项
< select ng-model ="selectedid" class ="form-control">< option ng-repeat ="id in editIdOptionsData" value ="{{id}}"> {{id}}</option></select>
代码中的"id"是一个完整的对象.您应该做的是:
< option ng-repeat ="id in editIdOptionsData" value ="{{id.id}}"> {{id.id}}</option>
或仅出于可读性
< option ng-repeat ="editIdOptionsData中的元素" value ="{{element.id}}"> {{element.id}}</option>
如果要根据某些条件选择一个选项,则可以使用ngSelected指令:
ng-selected =<您的条件>"
在您的示例中,将是类似的内容(如果我正确理解您的需求)
< select ng-model ="selectedid" class ="form-control">< option ng-selected ="selectedid == id.id" editIdOptionsData中的ng-repeat ="id" value ="{{id.id}}"> {{id.id}}</option></select>
如果那不是您的样子,那么您必须在问题中更加具体.
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?
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.
这篇关于如何使用ng-repeat显示来自API的所有值,而我只需要显示所有对象中的一个键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!