자, 이제 Angular를 배웠으므로 쉽게 이동하십시오. 다음 코드 은으로 작동하지만 더 나은 방법이 있어야합니다. 나는 내가 할 수있는 모든 것을 읽고있다. 그리고 내가 말할 수있는 것에서, 아마 이것들을 공장에서 설정하는 것이 최선일 수 있을까 ?? 지금까지, 내가 시도한 모든 것은 끝내 버리는 것들을 끝내지 만, 아마 나에게 잘못되어있는 것일지도 모른다.Angularjs - 여러 API 호출에 올바른 방법은 무엇입니까
전제 조건 내가 내가 3 API 호출의 총을 만드는 중이라서
- 사용하여 GET 방법 그러나 1 는 POST 방식을 사용하는
내 코드 :
$apiUrl = '_includes/gtmetrix.php'; // This is using a local proxy to access remote API.
$apiKey = '';
$gapiUrl = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed';
$gapiKey = '1z2x3c4v5b6n7m8a9s';
$gapiStrategy = 'mobile';
$requestUrl = '<?php echo $_POST['url']; ?>';
function FetchCtrl($scope, $http, $templateCache) {
$scope.method = 'POST';
$scope.url = $requestUrl;
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
$http({method: $scope.method, url: $apiUrl + '?url=' + $scope.url, cache: $templateCache}).
success(function(data, status) {
$scope.status = status;
$scope.data = data || "Request successful";
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
$scope.fetch();
$scope.fetchGoogle = function() {
$scope.code = null;
$scope.response = null;
$http({method: 'GET', url: $gapiUrl + '?url=' + $scope.url + '&key=' + $gapiKey, cache: $templateCache}).
success(function(data, status) {
$scope.status = status;
$scope.datag = data || "Request successful";
}).
error(function(data, status) {
$scope.datag = data || "Request failed";
$scope.status = status;
});
};
$scope.fetchGoogle();
$scope.fetchGoogleMobile = function() {
$scope.code = null;
$scope.response = null;
// $scope.data = '<i class="fa fa-spinner fa-spin"></i>';
$http({method: 'GET', url: $gapiUrl + '?url=' + $scope.url + '&key=' + $gapiKey + '&strategy=' + $gapiStrategy, cache: $templateCache}).
success(function(data, status) {
$scope.status = status;
$scope.datagm = data || "Request successful";
}).
error(function(data, status) {
$scope.datagm = data || "Request failed";
$scope.status = status;
});
};
$scope.fetchGoogleMobile();
$scope.updateModel = function(method, url) {
$scope.method = method;
$scope.url = url;
};
}
나는 지금 올바른 방식으로 도움을 주시면 대단히 감사하겠습니다. 고마워!
우선, $ q.all과 요청을 결합하여 콜백에 하나의 함수 만 사용할 수 있습니다. 또한 ngResource 모듈을 사용하여 API 호출을 추상화하는 옵션이 있습니다. –