angularJS $http 服務

AngularJS $http

AngularJS $http 是一個用於讀取web服務器上數據的服務。php

$http.get(url) 是用於讀取服務器數據的函數。html

$http 是 AngularJS 中的一個核心服務,用於讀取遠程服務器的數據。
angularjs

如下是存儲在web服務器上的 JSON 文件:

 

應用解析:web

注意:以上代碼的 get 請求是本站的服務器,你不能直接拷貝到你本地運行,會存在跨域問題,解決辦法就是將 Customers_JSON.php 的數據拷貝到你本身的服務器上,附:PHP Ajax 跨域問題最佳解決方案ajax

AngularJS 應用經過 ng-app 定義。應用在 <div> 中執行。跨域

ng-controller 指令設置了 controller 對象 名。數組

函數 customersController 是一個標準的 JavaScript 對象構造器服務器

控制器對象有一個屬性: $scope.namesapp

 

$http.get() 從web服務器上讀取靜態 JSON 數據函數

服務器數據文件爲:  http://www.runoob.com/try/angularjs/data/Customers_JSON.php

當從服務端載入 JSON 數據時,$scope.names 變爲一個數組。

  <div ng-app= " myApp "  ng-controller= " customersCtrl "
<ul>
  <li ng-repeat= " x in names ">
    {{ x.Name +  ' ' + x.Country }}
  </li>
</ul>

</div>

<script>
var app = angular.module( ' myApp ', []);
app.controller( ' customersCtrl ', function($scope, $http) {
  $http. get( " http://www.runoob.com/try/angularjs/data/Customers_JSON.php ")   .success(function (response) {$scope.names = response.records;}); }); </script>
相關文章
相關標籤/搜索