在html模板中将json值传递给OrderBy

时间:2014-10-11 15:37:58

标签: angularjs

我迭代一个对象并尝试根据ColPos值对标题进行排序。但是这不起作用而且没有设置orderby我该如何解决这个问题?

<th ng-repeat="(key, value) in instancesData.Attributes | orderBy:'value.ColPos'" >
     <span>{{key}}</span>
</th>

演示:http://plnkr.co/edit/X90n24vx2wqKvXZuDYLJ?p=preview

1 个答案:

答案 0 :(得分:0)

正如我在评论中已经说过的那样:您尝试将过滤器orderByobject一起使用,但此过滤器仅适用于Arrays,您必须更改模型为Array或制作您自己的filter

我将为您提供第一个选项的解决方案。

在您的控制器中执行以下操作:

$scope.dataAttributesArray = 
   Object.keys($scope.instancesData.Attributes).map(function(key){
      return {key:key, val:$scope.instancesData.Attributes[key]};
   });

然后在您看来,您可以这样做:

<th ng-repeat="attribute in dataAttributesArray | orderBy:'val.ColPos'" data-x="{{value.ColPos}}" >
        <span>{{attribute.key}}</span>
</th>

Working Example