Angular Api获取请求问题

时间:2019-03-22 10:46:25

标签: angularjs typescript

所以我想使用angular从此api获取某个值:

https://ergast.com/api/f1/2013/driverStandings.json

我目前正在使用

app.controller("api", function($scope, $http) {
    $scope.home = "This is the homepage";

    $scope.getRequest = function() {
      console.log("I've been pressed!");
      $http.get("https://ergast.com/api/f1/2013/driverStandings.json").then(
        function successCallback(response) {
          //$scope.response = response.data;
          console.log("response.data.MRData.StandingsTable");
          console.log(response.data.MRData.StandingsTable);
          console.log("response.data.MRData.StandingsTable.StandingsLists.0.DriverStandings");
          console.log(response.data.MRData.StandingsTable.StandingsLists.DriverStandings);

        },
        function errorCallback(response) {
          console.log("Unable to perform get request");
        }
      );
    };
  });

这就是给我的回报

the return i get with the current code

以0的方式我不知道如何传递该值,它不会让我将其放入请求中,而我想获取该api中的DriverStandings数组。

2 个答案:

答案 0 :(得分:0)

啊,答案很简单:(就这样:

console.log(response.data.MRData.StandingsTable.StandingsLists [0]);

因为它是一个数组。

答案 1 :(得分:0)

只需使用:response.data.MRData.StandingsTable.StandingsLists [0] .DriverStandings 因为StandingsLists是一个数组,所以为[0]。