我试图根据存储多级结构的对象生成一个语句,如:
{ "reptype0": {"index":0, "value":{...}},"reptype1": {"index":1, "value":{...}},....}
用于带有角度ngOptions的标记,例如:
<select id="repType"
name="repType"
ng-model="representationType.rtmodel"
ng-options="reptype for (reptype,repsubtypes) in representationTypes">
<option value=''>--select a value--</option>
</select>
我试图弄清楚如何通过&#34; index&#34;的值来订购选项。属性,但我不能使&#39; orderBy&#39;正在寻找阵列。
答案 0 :(得分:0)
正如您已经发现的那样,orderBy
会查找数组而不是对象。每当我遇到这个问题时,我总是将对象转换为数组并使用它。
<强>控制器强>
$scope.getRepresentationTypesArray = getRepresentationTypesArray;
function getRepresentationTypesArray () {
var representationTypes = {};
angular.forEach($scope.representationTypes, function (type, subTypes) {
representationTypes[type] = [];
angular.forEach(subTypes, function (subType) {
representationTypes[type][subType.index] = subType.value;
});
});
return representationTypes;
}
查看强>
<select id="repType"
name="repType"
ng-model="representationType.rtmodel"
ng-options="reptype for (reptype,repsubtypes) in getRepresentationTypesArray()">
<option value=''>--select a value--</option>
</select>
getRepresentationTypesArray()
方法会以以下格式返回您的数据:
{ "reptype0": [ {...} ], "reptype1": [ {...} ], ....}
数据已经按索引排序,您可以直接在ng-options
。