如何在AngularJS中动态分配过滤器

时间:2015-05-18 09:49:38

标签: jquery angularjs

我想过滤以下角色的玩家是html:

 <li role="presentation" ng-repeat="x in availablePlayers | unique:'PlayerRole'" >
    <a href="#pg" aria-controls="pg" role="tab" data-toggle="tab" ng-model="player.PlayerRole">{{x.PlayerRole}}</a>
</li>

这是球员名单

 <tr ng-repeat="x in availablePlayers | filter:player">
            <td data-title="POS">{{x.PlayerRole}}</td>
            <td data-title="Player"><a href="#" class="exclamation">{{x.PlayerName}}</a></td>
            <td data-title="OPP">BOS <span class="white-color">vs</span> Cle</td>
            <td data-title="played">6</td>
            <td data-title="FPPG">42.3</td>
            <td data-title="SALARY"><span class="red-color">${{x.Price}}</span></td>
            <td data-title=""><a href="#"><img src="<?php echo $GLOBALS['RootURL'] ?>images/plus.png" alt="plus"/></a></td>
        </tr>

1 个答案:

答案 0 :(得分:2)

您可以在过滤器属性中调用$scope function。这是我制作的plunker

<div ng-app>
  <div ng-controller="MainCtrl">

      <hr>
          <a ng-click="setFilter('John');">Set filter "John"</a>
      <hr>

      <table class="table">
        <tr><th>Name</th><th>Phone</th></tr>
        <tr ng-repeat="friend in friends | filter:getFilter()">
          <td>{{friend.name}}</td>
          <td>{{friend.phone}}</td>
        </tr>
      </table>
  </div>
</div>

你的范围:

function MainCtrl($scope, $http) {
    $scope.friends = [{name:'John', phone:'555-1276'},
                      {name:'Mary', phone:'800-BIG-MARY'},
                      {name:'Mike', phone:'555-4321'},
                      {name:'Adam', phone:'555-5678'},
                      {name:'Julie', phone:'555-8765'}];

    $scope.filter = "";

    $scope.getFilter = function () {
        return $scope.filter;
    };  

    $scope.setFilter = function (filter) {
        $scope.filter = filter;
    };
};