加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何在不改变ng-repeat的$index的情况下使用ng-fil

发布时间:2020-12-17 17:38:22 所属栏目:安全 来源:网络整理
导读:目前,如果我过滤,$index也会更新.因此,如果有500个结果,我过滤排名,也会更新.如何使索引列不更新? 这是我的代码: input ng-model="query.teamName" //divtable class="table" thead tr thRank/th thTeam/th thLocation/th thScore/th /tr /thead tbody tr
目前,如果我过滤,$index也会更新.因此,如果有500个结果,我过滤排名,也会更新.如何使索引列不更新?

这是我的代码:

<input ng-model="query.teamName" /></div>
<table class="table">
  <thead>
    <tr>
      <th>Rank</th>
      <th>Team</th>
      <th>Location</th>
      <th>Score</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="team in teams | filter:query | orderBy:orderByScore:reverseSort">
      <td><span style="color:white">{{$index+1}}</span></td>
      <td>{{team.teamName}}</td>
      <td>{{team.teamLocation}}</td>
      <td>{{team.teamPoints | number:2}}</td>
    </tr>
  </tbody>
</table>

控制器:

scoreboard.controller('scoreboardCtrl',function ($scope,$filter) {

     $scope.orderByScore = 'teamPoints';
     $scope.reverseSort = true;

     $scope.teams = [
          { "teamName": "motorboat skydive","teamLocation": "1189 King","teamPoints": 35.53},{ "teamName": "the grinders","teamPoints": 127.90},{ "teamName": "team forrec","teamPoints": 29.46},{ "teamName": "bikini finger","teamPoints": 21.98},{ "teamName": "la familia","teamPoints": 148.32},{ "teamName": "darkness is","teamPoints": 108.88},{ "teamName": "grinders","teamPoints": 167.95},{ "teamName": "discarded youth","teamPoints": 55.52}
          ];
     };

解决方法

Angular过滤器删除并添加一个新数组并更新ng-repeat,因此$index也将更新.相反,您可以初始化索引,ng-init =“idx = $index 1”并使用它. ng-init值永远不会被观看,也不会更新,但$index将根据数组中项目的迭代次数而改变

<tr ng-repeat="team in teams  | filter:query | orderBy:orderByScore:reverseSort" ng-init="idx = $index+1">
    <td><span>{{idx}}</span></td>

Plnkr

由于索引在你的情况下是至关重要的,因为它是排名最好的方法来处理这可能是从控制器本身添加索引.

$scope.teams = [
          { "teamName": "motorboat skydive","teamPoints": 55.52}
          ]
   .sort(function(itm1,itm2){ return itm2.teamPoints - itm1.teamPoints }) //Sort teams

   .map(function(itm,idx){ itm.index = (idx+1); return itm;  }); //assign rankings

我使用本机排序或者只是在控制器中使用角度orderByFilter本身为初始集合(并从视图中删除ng-init变量赋值的逻辑).因此,您不会运行运行时$index问题.

Plnkr2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读