我正在尝试ng-repeat嵌套对象的属性并对其进行排序,但是排序对我来说不起作用。
我已经看到了:如何使用嵌套属性在AngularJS中排序
但json结构不同,我无法正常工作。
我的代码:
<div ng-repeat="item in data | orderBy:order.allListPosition">
<div>{{item.name}} - {{item.order}}</div>
</div>
范围:
$scope.data = {
"78962": {
"id": "78962",
"name": "item 2",
"type": "blind",
"order": {
"allListPosition": "008",
"catListPosition": "059"
},
"data": {
"status": "stop",
"percent": 20
},
"longPress": {
"item": "78966",
"active": true
}
},
"78963": {
"id": "78963",
"name": "item 3",
"type": "coolmaster",
"order": {
"allListPosition": "053",
"catListPosition": "001"
},
"data": {
"status": 1,
"temp": 16,
"point": 25,
"mode": "cool",
"fan": 3
},
"longPress": {
"item": "78966",
"active": false
}
}
};
谁能告诉我我在做什么错?
非常感谢
阿维
这里有两个原因orderBy
不起作用:
orderBy
仅适用于数组,但是您将其应用于普通对象。orderBy
在表达式中提供一个字符串值。您正在给它order.allListPosition
,它等于$scope.order.allListPosition
(不存在)。要解决第一个问题,请添加一个数据对象数组:
$scope.dataArray = Object.keys($scope.data)
.map(function(key) {
return $scope.data[key];
});
要解决第二个问题(并结合dataArray
上面的内容):
<div ng-repeat="item in dataArray | orderBy:'order.allListPosition'">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句