我试图用Angular(我的第一个角度应用程序)发出get请求而且它没有工作
这是app.js文件
var app = angular.module('myApp',[]);
app.controller('getCtrl',['$scope',function($scope){
$http.get("http://127.0.0.1:3000/api/Users")
.then(function(response){
$scope.users = response.data;
});
}]);
和index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div ng-app="myApp">
<div ng-controller="getCtrl">
<table>
<tr ng-repeat = "user in users">
<td>{{user.name}}</td>
<td>{{user.lastName}}</td>
</tr>
</table>
</div>
</div>
<script src="node_modules/angular/angular.js"></script>
<script src="app.js"></script>
</body>
</html>
这是使用rest客户端的get请求的响应
[
{
"_id": "5787c061191313250ba4a7ab",
"created_at": "2016-07-14T16:40:01.076Z",
"updated_at": "2016-07-14T16:40:01.076Z",
"lastName": "apellido1",
"name": "user1",
"__v": 0
},
{
"_id": "5787c069191313250ba4a7ac",
"created_at": "2016-07-14T16:40:09.952Z",
"updated_at": "2016-07-14T16:40:09.952Z",
"lastName": "apellido2",
"name": "user2",
"__v": 0
},
{
"_id": "5787c071191313250ba4a7ad",
"created_at": "2016-07-14T16:40:17.518Z",
"updated_at": "2016-07-14T16:40:17.518Z",
"lastName": "apellido3",
"name": "user3",
"__v": 0
},
{
"_id": "5787c95b0b2e8aa6183044dc",
"created_at": "2016-07-14T17:18:19.643Z",
"updated_at": "2016-07-14T17:18:19.643Z",
"lastName": "apellido4",
"name": "nombre4",
"__v": 0
},
{
"_id": "5787ca740b2e8aa6183044de",
"created_at": "2016-07-14T17:23:00.229Z",
"updated_at": "2016-07-14T17:23:00.229Z",
"lastName": "apellido5",
"name": "nombre5",
"__v": 0
},
{
"_id": "5790ec389f3a7a0b0a603be2",
"created_at": "2016-07-21T15:37:28.671Z",
"updated_at": "2016-07-21T15:37:28.671Z",
"lastName": "apellido6",
"name": "nombre6",
"__v": 0
}
]
在网页上结果是空的,就好像请求是空的,我不知道我是否错过了某些东西
答案 0 :(得分:4)
var app = angular.module('myApp',[]);
app.controller('getCtrl',['$scope','$http',function($scope,$http){
$http.get("http://127.0.0.1:3000/api/Users")
.then(function(response){
$scope.users = response.data;
});
}]);
修复
将正确的$http
依赖项注入控制器构造函数
response.data