Setting up dynamic $http.get request (Angular)

2019-09-02 01:34发布

问题:

I want to know how to dynamically change an $http call so that the $http request URL differs based on the element that is clicked in an ng-repeat list. But I'm stuck.


Currently, I have an ng-repeat set up on my index page:

<div ng-repeat="team in nt.getStandings">
    <a href="#/nation-details/{{team.team_id}}"><h2>{{team.team_name}}</h2></a>
    <p>Team ID = {{team.team_id}}</p>
</div>

The variable getStandings is taken from an API via an $http call. Like so:

In StandingsService

return $http.get(
    'http://api.com/standings/1005?Authorization=xxxx'
) 

        .success(function(data) { 
          return data; 
        }) 
        .error(function(err) { 
          return err; 
        }); 

And then StandingsService is attached to the getStandings variable in my controller.

"1005" is a property which calls a specific array, in this case a particular sporting competition, from an array of competitions.

So, on my index page I'm using ng-repeat to list all teams within that competition.

As you can see on the html above, I have linked each team so that it dynamically generates a URL which appends the team_id to the end, which using $routeParams I define as the variable whichTeam.

Team Details Page

<h1>Dynamic Team ID = {{whichTeam}}</h1>

This works fine, the team ID is generated dynamically according the team that is clicked.


Just like 'StandingsService' above, I have another service called 'TeamService' which makes an $http request to pull team data. Currently though it is set up statically to make a call to one individual team - I want to make the service take in the whichTeam variable so that the call changes depending on which team was clicked.

This is the static team $http request (I've broken the URL apart and concatenated to make it clearer):

    return $http.get(
    'http://api.com/team/' + '16110' + '?Authorization=xxxx'
    ) 

I want the 16110 part, which refers to ONE team, to be a the whichTeam variable, allowing it to pull in the correct individual team data, but I don't know how to write this (or indeed if it's possible).

I hope I've been clear - happy to clarify further if needed. Thanks in advance.

回答1:

Make a factory:

app.factory("DataService", ["$http", function($http) {
    return {
        getTeamDetailsById: function(teamId) {
            return $http.get('path/to/api' + teamId + '?Auth=xxxx')
        }
    };
}]);

Use it in a controller:

app.controller("MainCtrl", ["$scope", "DataService", function($scope, DataService) {
    $scope.teamDetails = {};

    $scope.getTeamDetailsById = function(event, teamId) {
        //prevent click navigation
        event.preventDefault();

        //call factory service
        DataService.getTeamDetailsById(teamId).then(function(response) {
            //success callback
            $scope.teamDetails = response.data;
        }, function(response) {
            //an error has occurred
        });
    }
}]);

In the ng-repeat element:

<div ng-repeat="team in teams">
    <a href ng-click="getTeamDetailsById($event, team.team_id)">{{team.team_name}}</a>
</div>

The above assumes you have only one state and are storing in only one controller. If you want to use different states usving $stateProvider, then you'd have to use parameters, by making use of ui-sref and passing in team Id.

If indeed you are using $states and parameters, do this:

<a href ng-click="goToState($event, team.team_id)">{{ team.team_name }}</a>

$scope.goToState = function(e, teamId) {
    $state.go("teamDetailsState", { "teamId": teamId });
}