2016-08-29 4 views
0

서비스를 사용하여보기의 입력 필드에서 값을 전달해야합니다. 이 서비스는 내 WebAPI2으로 전화를 한 다음 유효한 JSON을 응답으로 받아야합니다.AngularJS : 서비스 호출 API 및 JSON 반환

그러나 약속 대상 개체를 가져 오는 중 (심지어 ".then()"으로 해결할 수 없음) 또는 공장을 사용중인 경우 $get 메서드를 구현하지 않는 것처럼 컴파일되지 않습니다.

JSON 반환하는 개체도 유효합니다.

보기 :

<div class="input" ng-controller="InputController as ctrl"> 
<input ng-model="inputdata" /> {{inputdata}} 
    <button ng-click="ctrl.gibDaten(inputdata)">Senden</button> 
    {{cooledaten}} 
</div> 

컨트롤러 :

module GoogleMapsApp { 
myApp.myAppModule.controller("InputController", 
    ["$scope", "mapsServiceCustom", 
     function ($scope, mapsServiceCustom) { 
      $scope.lEingabedaten = $scope.inputdata; 
      this.gibDaten = function() { 
       $scope.cooledaten = mapsServiceCustom.gibDaten().then(); 
       console.log($scope.cooledaten); 
      } 
}]); 

}

서비스 :

module GoogleMapsApp { 
myApp.myAppModule.service("mapsServiceCustom", 
    ["$http", 
     function ($http) { 
      this.gibDaten = function (lEingabe) { 
       console.log(lEingabe); 
       console.log($http.get("api/getDistanceData/" + lEingabe + "/Cologne") 
        .then(function(data) { 
         return data; 

컨트롤러 :}))); return $ http.get ("api/getDistanceData /"+ eEngine + "/ Cologne") .then (function (data) { 데이터 돌아 가기, }); // $ http.get ("api/getDistanceData /"+ lEingabedaten1 + "/ Cologne")을 반환합니다. } } ]);

}

콘솔 로그 : 나는 $$ 상태를 확인하는 경우

Object { $$state: Object }maps.serviceCustom.js:14:17

Object { $$state: Object }InputController.js:8:17

: 정확히 원하는 데이터를 포함하는 객체. 공장을 사용

다음과 같은 오류 날 리드 : 그래서 내가 뭘 잘못

https://docs.angularjs.org/error/$injector/undef?p0=mapsServiceCustom

? 내 의도를 어떻게 구현합니까?

답변

0

당신은 당신은 제대로 서비스를 구현하지 않은

myApp.myAppModule.controller("InputController", 
    ["$scope", "mapsServiceCustom", 
     function ($scope, mapsServiceCustom) { 
      $scope.lEingabedaten = $scope.inputdata; 
      this.gibDaten = function() { 
       mapsServiceCustom.gibDaten().then(function(response){ 
        // set response to your variable 
        $scope.cooledaten = response; 
       }); 
      } 

}]); 
0

... ... 다음 부분에 컨트롤러에 그러니 그냥 넣어 다음 기능을하는 함수를 통과하고 변수에 응답을 반환 설정해야합니다. 다음 링크 (AngularJS와 공식 문서)를 통해 이동하시기 바랍니다 당신은 그에게 JS 객체를 반환하여 API를 노출 할 수 있습니다. (같은 일이 당신의 오류 링크에 표시됩니다. 그뿐만 아니라 참조)

필요 https://docs.angularjs.org/guide/services

여기

module GoogleMapsApp { 
myApp.myAppModule.service("mapsServiceCustom", 
    ["$http", function ($http) { 
      this.gibDaten = function (lEingabe) { 
       console.log(lEingabe); 
       console.log($http.get("api/getDistanceData/" + lEingabe + "/Cologne") 
        .then(function(data) { 
         return data; 
        } 
      } 

      return { gibDaten: this.gibDaten}; 
    }]); 
} 

당신은 더 많은 것을 배울 수 $ 방법을 얻을 https://docs.angularjs.org/guide/providers