javascript – AngularJs ng-repeat orderBy不适用于嵌套对象属性

前端之家收集整理的这篇文章主要介绍了javascript – AngularJs ng-repeat orderBy不适用于嵌套对象属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图重复嵌套对象属性并对它们进行排序,但排序对我不起作用.

我见过这个:
How to orderby in AngularJS using Nested property

但是json结构是不同的,我无法让它工作.

Plunker

我的代码

<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'">

http://plnkr.co/edit/BXgYPTElSM3sjvLg30CL?p=preview

原文链接:https://www.f2er.com/js/155221.html

猜你在找的JavaScript相关文章