复杂的ng-repeat orderBy

时间:2015-06-13 03:07:13

标签: angularjs

[{"score":"0.995074","content":"Video Games"},{"score":"0.950704","content":"Media"},{"score":"0.916667","content":"Arts & Entertainment"}]

根据每个ng-repeat项目提供的上述数据,我希望只能score"content":"Video Games"进行排序。我怎么能这样做?

编辑澄清:

第1项

{ property1: value1, property2: value2, ..., scores: [{"score":"0.9","content":"Video Games"},{"score":"0.8","content":"Media"},{"score":"0.7","content":"Arts & Entertainment"}] }

第2项

{ property1: value1, property2: value2, ..., scores: [{"score":"0.7","content":"Video Games"},{"score":"0.8","content":"Media"},{"score":"0.9","content":"Arts & Entertainment"}] }

第3项

{ property1: value1, property2: value2, ..., scores: [{"score":"0.8","content":"Video Games"},{"score":"0.7","content":"Media"},{"score":"0.9","content":"Arts & Entertainment"}] }

以上是我的数据的样子。我需要ng-repeat来对它们进行排序:项目1,项目3,项目2(降序)或项目2,项目3,项目1(升序)。

2 个答案:

答案 0 :(得分:1)

我想通过"以上数据可用于每个单独的ng-repeat项目"你的意思是每个项目都有一个属性scores,其中包含该格式的数据作为其值。也就是说,您的商品如下:

{
  property1: value1,
  property2: value2,
  ...,
  scores: [{"score": "0.995074", "content": "Video Games"}, ...]
}

那么你可以使用" getter功能" orderBy的特征。首先在您的范围内准备以下功能:

$scope.videoGamesScore = function (item) {
  for (var i in item.scores) {
    var rec = item.scores[i];
    if (rec["content"] == "Video Games")
      return rec["score"];
  }
}

使用此功能,您可以像这样编写ng-repeat

ng-repeat="item in items | orderBy:videoGamesScore"

答案 1 :(得分:-2)

<!DOCTYPE html> <html> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <body>

<div ng-app="" ng-init="names=[{'score':'0.995074','content':'Video Games'},{'score':'0.950704','content':'Media'},{'score':'0.916667','content':'Arts & Entertainment'}]">

<div ng-repeat="x in names | orderBy:'score'">          score : {{x.score}} ,content : {{x.content}} </div>



</body> </html>