AngularJS入门-(12)http

来源:互联网 发布:淘宝骗手机 编辑:程序博客网 时间:2024/06/16 18:42

http请求方式

$http({    method: 'GET',    url: '/someUrl'}).then(function successCallback(response) {        // 请求成功执行代码    }, function errorCallback(response) {        // 请求失败执行代码});

AngularJS httpAngularJShttp 是一个用于读取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>