从ng-repeat angularjs中的项目中删除/过滤数据

时间:2015-02-01 19:05:07

标签: angularjs

我想创建一个过滤器来从检索到的数据中删除特定的字符串,例如team.teamName会返回一个名称,例如" FC Arsenal London"或者"桑德兰AFC"但是,如果返回的数据包括FC,AFC或伦敦,那么我希望将其删除,因此它只会返回" Arsenal"或者"桑德兰"例如。我访问的JSON在这里:

http://www.football-data.org/alpha/soccerseasons/354/leagueTable?callback=JSON_CALLBACK

HTML:

<tr ng-repeat="team in teamsList">
        <td>{{$index + 1}}</td>

        <td><a href="#/teams/{{team._links.team.href }}">
              {{team.teamName}}
              </a></td>
        <td>{{team.playedGames}}</td>
        <td>{{team.points}}</td>
        <td>{{team.goals}}</td>
        <td>{{team.goalsAgainst}}</td>
        <td>{{team.goalDifference}}</td>
      </tr>

CONTROLLER.js

angular.module('PremierLeagueApp.controllers', []).
  controller('teamsController', function($scope, footballdataAPIservice) {
    $scope.teamsList = [];

    footballdataAPIservice.getTeams().success(function (response) {
        //Dig into the response to get the relevant data
        $scope.teamsList = response.standing;
    });
  });

1 个答案:

答案 0 :(得分:1)

您可以编写一个AngularJS filter来移除&#39; AFC&#39;和&#39; FC&#39;。

以下是一个例子:

.filter('removeteam', function () {
    return function (input) {
        return input.replace('AFC', '').replace('FC', '');
    }
});

通过将一些字段输入过滤器来调用它:

<li ng-repeat="team in teams">{{ team.name | removeteam }}</li>

这是一个显示它的小提琴:http://jsfiddle.net/cuyw8a4b/1/