1

I want to create a filter to retrieve only the last digits from a URL, for example team._links.team.href gives back a URL such as http://api.football-data.org/alpha/teams/61 but I want only want it to give back 61. How could I do this?

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

2 Answers 2

2

You can add a simple function on the controller like this:

$scope.teamID = function(url) {
    return url.substr(url.lastIndexOf('/') + 1);
};

and then use it in your repeat:

<a href="#/teams/{{teamID(team._links.team.href)}}">

Here's a fiddle.

Sign up to request clarification or add additional context in comments.

Comments

1

One way to go is to define your own filter for this. For example like this:

.filter('idOnly', function() {
  return function(input) {
    return input.substring(input.lastIndexOf('/')+1)
  };
})

which then can be used like this:

<td>
  <a href="#/teams/{{team._links.team.href | idOnly }}">
              {{team.teamName}}
  </a>
</td>

A full code snippet is added below.

angular.module('PremierLeagueApp', [])

.filter('idOnly', function() {
  return function(input) {
    return input.substring(input.lastIndexOf('/')+1)
  };
})

.controller('teamsController', function($scope) {
  $scope.teamsList = [ { teamName : 'team1',
                         playedGames : 1,
                         points: 0,
                         goals: 1,
                         goalsAgainst: 4,
                         goalsDifference: 3,
                         _links : {
                             team : { href : "http://api.football-data.org/alpha/teams/61"}
                         }
                       },
                       { teamName : 'team2',
                         playedGames : 1,
                         points: 3,
                         goals: 4,
                         goalsAgainst: 1,
                         goalsDifference: 3,
                         _links : {
                             team : {href : "http://api.football-data.org/alpha/teams/62"}
                         }
                       }                     
                     ];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="PremierLeagueApp">
  
<div ng-controller="teamsController">{{aap}}
<table>
       <tr ng-repeat="team in teamsList">
        <td>{{$index + 1}}</td>

        <td><a href="#/teams/{{team._links.team.href | idOnly }}">
              {{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>
</table>
</div>  
</body>  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.