2016-09-21 2 views
0

나는 2 개 공장이 있습니다.각도 공장 종속성 문제

는 ApiService에서 나는 LocationService 사용 것이라는 $의 HTTP 호출에서 엔드 포인트를 반환하고 싶습니다.

는하지만 컨트롤러가 LocationService를 호출 할 때, 그것은 ApiService의 응답을 기다리지 않습니다 보인다. 여기에 일부 코드 조각은, ApiService에서 나는 마침내 그것이 내가 엔드 포인트 얻기 위해 서버를 호출 할 때마다 할 필요가 없습니다 그래서 그것을 캐시하는 작업을 진행하는 경우 : 여기

services.factory("ApiService", ["$location", "$http", function ($location, $http) { 
    return { 
     getEndpointUrl: function() { 
      var endpoint; 

      $http({ 
       method: 'GET', 
       url: '/site/apiendpoint' 
      }).then(function successCallback(response) { 
       endpoint = response.data; 
       console.log(endpoint); 
       return endpoint; 
      }, function errorCallback(response) { 
       console.error('Error retrieving API endpoint'); 
      }); 
     } 
    } 
}]); 

위치 서비스입니다,

services.factory("LocationService", ["$resource", "ApiService", function ($resource, apiService) { 
    var baseUri = apiService.getEndpointUrl(); 
    return $resource(baseUri + '/location', {}, { 
     usStates: { method: 'GET', url: baseUri + '/location/us/states' } 
    }); 
}]); 

내 컨트롤러는 기저 URI가 정의되지 LocationService.usStates을 호출하려고 : 그것은 ApiService 소모합니다. 여기서 내가 뭘 잘못하고 있니? ApiService에서

답변

2

이유는, 그것은 더 반환 값이 없습니다.

angular.module('yourModule').constant('baseUrl', window.baseUrl); 

이 그런 다음 서비스에 주입하는 것입니다 : 당신의 LocationService 이후

은 $ 리소스를 사용하고 baseUri에 의존, 나는 초기 페이지로드와는 일정한 같이하고 함께 데이터가 부트 스트랩을 제안 귀하의 리소스를 만드십시오 :

services.factory("LocationService", ["$resource", "ApiService", "baseUrl", function ($resource, apiService, baseUrl) { 
     return $resource(baseUrl + '/location', {}, { 
      usStates: { method: 'GET', url: baseUrl + '/location/us/states' } 
     }); 
    }]); 
+0

안녕하세요, 이것은 완벽 할 것입니다. 그러나 어떻게 그 상수를 동적으로 처음으로 설정합니까? 다른 엔드 포인트에서 엔드 포인트를 가져와야하므로, 엔드 포인트가 환경에 따라 다를 수 있습니다. – TheWebGuy

+0

어떤 서버 측 기술을 사용하고 있습니까? – DerekMT12

+0

.NET MVC (API 엔드 포인트 반환) 및 다른 웹 API 2 프로젝트 (엔드 포인트). 나의 초기 아이디어는 URL (location.host())을 기반으로 엔드 포인트를 설정하는 것이지만 더 깨끗한 것을 찾고 있는데, web.config에 설정이있다. – TheWebGuy

0

, 당신은 실제로 getEndpointUrl()에서 값을 반환하지 않는 것입니다. 방법에 대해 당신은 ApiService에서 약속을 반환하고 소비 동기 방식에서 LocationService인가? 컨트롤러에서

services.factory("ApiService", ["$location", "$http", function($location, $http) { 
    return { 
     getEndpointUrl: function() { 
      var endpoint; 

      return $http({ 
       method: 'GET', 
       url: '/site/apiendpoint' 
      }); 
     } 
    } 
}]); 

services.factory("LocationService", ["$resource", "ApiService", function($resource, apiService) { 
    return { 
     getLocations: function() { 
      return apiService.getEndpointUrl().then(function successCallback(response) { 
       var baseUri = response.data; 

       return $resource(baseUri + '/location', {}, { 
        usStates: { method: 'GET', url: baseUri + '/location/us/states' } 
       }); 

      }, function errorCallback(response) { 
       console.error('Error retrieving API endpoint'); 
      }); 
     } 
    }; 
}]); 

: 그리고 당신의 getEndpointUrl 기능은 비동기이기 때문에

LocationService.getLocations().then(function(data) { 
    $scope.statesResult = data.result.states; 
});