이 기사에서는 AngularJS를 사용하여 HTTP 요청을 원격 API에 요청하고 JSON 응답으로보기를 업데이트하는 방법을 보여줍니다. Angular의 서비스 및 데이터 바인딩을 활용합니다 주요 개념 :
$http
: 쿼리 문자열을 포함하여 API에 요청을 보내는 데 사용되었습니다. 응답은 모델에 저장됩니다 (이 예에서는
및$http.get
>이 경우 모델 업데이트 (이 경우 800ms)를 지연시켜 과도한 API 호출을 방지합니다.
서비스 : details
변경 사항에 대한 related
모델을 모니터링하여 데이터 가져 오기를 유발합니다.
ngModelOptions
이 프로젝트는 모듈 식 구조 (CSS, JS, PARTIALS, Index.html)를 사용합니다. debounce
모델에 바인딩)과 부분 뷰 ($watch
는 를 사용하여 OMDB API로 두 개의 API 호출 (메인 영화 세부 사항, 관련 영화 용)을 작성합니다. 그런 다음 응답은 search
및 .then()
를 .success()
debounce로 대체하고 .
예제 코드 스 니펫 :
ng-model
in ng-model-options
: index.html
<code class="language-html"><input type="text" ng-model="search" ng-model-options="{ debounce: 800 }" placeholder="Enter full movie name"></code>
fetch()
app.js
<code class="language-javascript">function fetch() {
$http.get("http://www.omdbapi.com/?t=" + $scope.search + "&tomatoes=true&plot=full")
.then(function(response) { $scope.details = response.data; });
$http.get("http://www.omdbapi.com/?s=" + $scope.search)
.then(function(response) { $scope.related = response.data; });
}</code>
조건부 이미지 로딩 :
main-info.html
위 내용은 Angular ' s $ http 서비스를 사용하여 API 호출의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!