AngularJS 内置服务 ~ $http().then()

来源:互联网 发布:gps车辆监控软件 编辑:程序博客网 时间:2024/06/05 16:11
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AngularJS 内置服务 ~ $http().then() </title>
<script type="text/javascript" src="../js/angular.js" ></script>
<script>
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope,$http){
/* 
* $http:服务向服务器发送请求,应用响应服务器传过来的数据。
* 语法:$http().then()
$http()指定请求参数信息;
then()第一个参数是请求成功的执行函数,第二个参数是请求失败的执行函数。*/

$http({
method:"get",
url:"myJson.json"
}).then(function success(respsone){
$scope.users = respsone.data;
},function error(){
console.log("获取失败!");
});
});
</script>
</head>

<body ng-app="myApp" ng-controller="myCtrl">
<center>
<table border="1" bordercolor="blue" cellpadding="18" cellspacing="0">
<caption>用户信息表<br><br></caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.age}}</td>
</tr>
</tbody>
</table>
</center>
</body>
</html>
原创粉丝点击