orderBy:notarray预期数组但收到:对象

时间:2016-08-09 20:05:03

标签: html angularjs firebase firebase-realtime-database

我正试图按照它的位置过滤我的结果。

但是,一旦orderby打破了页面,因为结果不是数组。我怎么能改变这个?我坚持这个。

我是这样做的:

<div ng-repeat="cat in content | orderBy : 'pos'" class="animated zoomIn">

控制器:

firebase.database().ref("v3/standalonelist/" + catId.toLowerCase()).on('value', function(snapshot) {

    $timeout(function() {
      $scope.content = snapshot.val();
      console.log($scope.content);

    })

  });

$ scope.content返回此对象:

{
  "Bar One" : {
  "bgurl" : "https://domain.com/aa/bars/list/bar.jpg",
  "name" : "Bar One",
  "subtitle" : "Cheers",
  "pos", 2

},
"Bar Two" : {
  "bgurl" : "https://domain.com/aa/bars/list/bar.jpg",
  "name" : "Bar Two",
  "subtitle" : "Cheers",
  "pos", 1
 }

}

我得到了这个错误:

  

[orderBy:notarray]预期数组但收到:[上面的对象]

2 个答案:

答案 0 :(得分:1)

1) 自定义过滤器(AngularJS) for orderBy对象属性..

示例JSON:

{ 
   "123": {"name": "Test B", "position": "2"},
   "456": {"name": "Test A", "position": "1"}
}

试试此链接...

orderBy filter for Object

2)另一种实现orderBy过滤对象属性的方法。

  • 添加数据对象数组:

$scope.dataArray = Object.keys($scope.data) .map(function(key) { return $scope.data[key]; });

在视图中:

<div ng-repeat="item in dataArray | orderBy:'order.allListPosition'">

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

答案 1 :(得分:0)

您建模的对象不是数组,因此您应该使用(key, value) in experssion synatx。