AngularJS入門-(12)http

http請求方式

$http({
    method: 'GET',
    url: '/someUrl'
}).then(function successCallback(response) {
        // 請求成功執行代碼
    }, function errorCallback(response) {
        // 請求失敗執行代碼
});

AngularJS httpAngularJS http 是一個用於讀取web服務器上數據的服務。
$http.get(url) 是用於讀取服務器數據的函數。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.6.3/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp" ng-controller="siteCtrl"> 

<ul>
  <li ng-repeat="x in names">
    {{ x.name + ', ' + x.age }}
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('siteCtrl', function($scope, $http) {
  $http.get("請求地址")
  .then(function (response) {$scope.names = response.data.sites;});
});
</script>

</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章