如何使用AngularJS orderBy

时间:2014-10-09 02:26:09

标签: html css angularjs angularjs-filter

我认为我的语法不正确?我可以在Angular中这样做吗?前两个过滤器有效,但不是按顺序

<div class ="col-md-8 pull-left">
      <span ng-click="myFilter = {green: true}">Clean</span>  |
      <span ng-click="myFilter = {green: false}">Dirty</span> |
      <span ng-click="myFilter = orderBy: 'governance' : true">Goverance</span> |
      <span ng-click="myFilter = orderBy: 'environmental' : true ">Environmental</span> |
      <span ng-click="myFilter = orderBy: 'community' :true">Community</span> |
      <span ng-click="myFilter = null">All</span>
</div>

<div class="list-group-item col-md-8 pull-left" ng-repeat="site in list.links | filter:myFilter">
{{site.governance}}
{{site.environmental}}
{{site.community}}
</div>

示例对象是

{
      name: 'Facebook',
      url: 'www.facebook.com',
      green: false,
      governance: 45,
      environment: 69,
      community: 72,
      rank: 6
    }

2 个答案:

答案 0 :(得分:2)

你的语法错误,你不能这样设置,filter和orderBy是两个不同的过滤器,并采用2个不同的表达式。而是在点击时设置订单变量。

<div class ="col-md-8 pull-left">
     ...
      <span ng-click=" order='governance' ">Goverance</span> |
      <span ng-click=" order='environmental' ">Environmental</span> |
      <span ng-click=" order='community' ">Community</span> |
     ...
</div>

并使用变量: -

ng-repeat="site in list.links | filter:myFilter |orderBy:order"

也许还有一种: -

 <span ng-click=" order='governance'; sort=!sort ">Goverance</span>
....
<div .. ng-repeat="site in list.links | filter:myFilter |orderBy:order:sort"

<强> Plnkr

答案 1 :(得分:0)

正如PSL所说,你可以通过点击设置变量来实现,在orderBy子句中使用该变量。

Here is a working fiddle

<div>
  <span ng-click="myFilter = {green: true}">Clean</span>  |
  <span ng-click="myFilter = {green: false}">Dirty</span> |
  <span ng-click="myOrder = 'governance'">Goverance</span> |
  <span ng-click="myOrder = 'environmental' ">Environmental</span> |
  <span ng-click="myOrder = 'community'">Community</span> |
  <span ng-click="myFilter = null">All</span>
</div>

<div class="list-group-item col-md-8 pull-left" ng-repeat="site in list.links | filter:myFilter | orderBy:myOrder"></div>