如何谓词过滤器,而不是使用 2 ng-ifs?

How to predicate filter, instead of using 2 ng-ifs?

.teams(ng-repeat='team in service.teams' ng-class="{'last':$last}")
          .row
            .team.roster(ng-click='showPlayers = !showPlayers')
              .col-xs-7.col-sm-4.col-md-4.mainRoster-col
                .team-name
                  {{team.name}}

      //shows the player rating for the highest rated player

      .roster(ng-repeat="player in team.playerProfiles | orderBy: '-pRating' | limitTo:1" ng-if="!team.selected")
        .row
          .col-xs-1.col-sm-1.col-md-1.rosterCheck-col
          .col-xs-7.col-sm-7.col-md-7
            %table
               %tr
                 %td
                  .p_rating
                    {{player.pRating | percentage}}


       //shows a selected player, instead of showing the default player with highest rating

      .roster(ng-repeat="player in team.playerProfiles" ng-if="team.selected && team.selected.playerId == player.id")
        .row
          .col-xs-1.col-sm-1.col-md-1.rosterCheck-col
          .col-xs-7.col-sm-7.col-md-7
            %table
              %tr
                %td
                  .p_rating
                    {{player.pRating | percentage}}

以上示例有效,我只是不确定这是否是获得我想要实现的目标的最佳方式?我应该尝试使用过滤器还是开关?或者,这是'acceptable'?


所以,我正在接受评论的建议,但我在某处出错了。我正在尝试在过滤器中使用谓词函数:似乎我无法通过 player.id.. 对团队中的玩家进行 for 循环... ?

roster(ng-repeat="player in team.playerProfiles | orderBy: '-pRating' | limitTo:1 | filter:criteriaMatch(team, player)")
    .row
      .col-xs-1.col-sm-1.col-md-1.rosterCheck-col
      .col-xs-7.col-sm-7.col-md-7
        %table
           %tr
             %td
              .p_rating
                {{player.pRating | percentage}}

然后,在我的控制器中:

$scope.criteriaMatch = function( team, player ) {
  if (team.selected)
    return player.id == team.selected.playerId;
    };
  else 
    return player.id
};
roster(ng-repeat="player in team.playerProfiles | orderBy: '-pRating' | limitTo:1 | filter:team.selected.playerId)")

根据您已有的知识,它可能没有您想象的那么复杂。您可以仅按过滤器内的 selected.playerId 内联进行过滤。