אVי*_*אVי 6 javascript angularjs
我试图重复嵌套对象属性并对它们进行排序,但排序对我不起作用.
我已经看到了: 如何使用嵌套属性在AngularJS中进行排序
但是json结构是不同的,我无法让它工作.
我的代码:
<div ng-repeat="item in data | orderBy:order.allListPosition">
<div>{{item.name}} - {{item.order}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
范围:
$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
}
}
};
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我我做错了什么?
非常感谢
阿维
JLR*_*she 14
有两个原因orderBy在这里不起作用:
orderBy 仅适用于数组,但您将它应用于普通对象.orderBy a string value with the expression. You are giving it order.allListPosition, which would equate to $scope.order.allListPosition (which doesn't exist).To solve the first issue, add an array of your data objects:
$scope.dataArray = Object.keys($scope.data)
.map(function(key) {
return $scope.data[key];
});
Run Code Online (Sandbox Code Playgroud)
To solve the second issue (and incorporate the dataArray from above):
<div ng-repeat="item in dataArray | orderBy:'order.allListPosition'">
Run Code Online (Sandbox Code Playgroud)
http://plnkr.co/edit/BXgYPTElSM3sjvLg30CL?p=preview