2016-12-23 111 views
0

我正在研究一個使用NASA Open API的小應用程序。我目前每個請求都有一個控制器,具體取決於漫遊者的照片將被退回。我希望將這兩個控制器組合成一個,同時可以選擇返回照片的漫遊者。在角度控制器中組合兩個API請求

app.controller('OpportunityController', function($scope, $http) { 

    // get date 
    let today = new Date(); 
    let dd = today.getDate() - 5; 
    let mm = today.getMonth() + 1; 
    let yyyy = today.getFullYear(); 

    if (dd < 10) { 
     dd = '0' + dd 
    } 
    if (mm < 10) { 
     mm = '0' + mm 
    } 
    today = yyyy + '-' + mm + '-' + dd; 

    // set variables 
    $scope.baseUrl = "https://api.nasa.gov/mars-photos/api/v1/rovers/"; 
    $scope.rover = ["Opportunity"]; 
    $scope.date_params = "/photos?earth_date=" + today; 
    $scope.searchParams = $scope.rover + $scope.date_params; 
    $scope.key = "&api_key=API_KEY"; 

    // request 
    $http.get($scope.baseUrl + $scope.searchParams + $scope.key) 
     .success(function(result) { 
      $scope.photos = result.photos; 
      console.log($scope.photos); 
     }) 
     .error(function(error) { 
      console.log(error); 
     }); 
}); 

app.controller('MarsController', function($scope, $http) { 

    // get date 
    let today = new Date(); 
    let dd = today.getDate()-1; 
    let mm = today.getMonth()+1; 
    let yyyy = today.getFullYear(); 

    if(dd<10) { 
     dd='0'+dd 
    } 
    if(mm<10) { 
     mm='0'+mm 
    } 
    today = yyyy+ '-' +mm+ '-' +dd; 

    // set variables 
    $scope.baseUrl = "https://api.nasa.gov/mars-photos/api/v1/rovers/"; 
    $scope.rover = ["Curiosity"]; 
    $scope.date_params = "/photos?earth_date=" + today; 
    $scope.searchParams = $scope.rover + $scope.date_params; 
    $scope.key = "&api_key=API_KEY"; 

    // request 
    $http.get($scope.baseUrl + $scope.searchParams + $scope.key) 
     .success(function(result) { 
      $scope.photos = result.photos; 
      console.log($scope.photos); 
     }) 
     .error(function(error){ 
      console.log(error); 
     }); 
}); 

回答

0

沒有看到一個問題

$http.get($scope.baseUrl + $scope.searchParams + $scope.key) 
     .success(function(result) { 
      $scope.photos = result.photos; 
      console.log($scope.photos); 
     }) 
     .error(function(error) { 
      console.log(error); 
     }); 
$http.get($scope.baseUrl + $scope.searchParams + $scope.key) 
     .success(function(result) { 
      $scope.photos = result.photos; 
      console.log($scope.photos); 
     }) 
     .error(function(error) { 
      console.log(error); 
     });