我正在使用 Rest 在本地主机上以 Json 格式显示数据库中的信息。网址是http://localhost:8080/restful-services/api/getAllHorses .
我是 Angular 新手,我想做的是使用 http get 来访问 json 信息。
这是我的 json
[{"horse":{"age":"6yo","breeder":"David Harvey","countryBred":"(IRE)","horseAge":"6yo","horseId":1,"horseName":"Brain Power","owner":"Michael Buckley","pedigree":"Kalanisi","sex":"(08May11 b g)","trainer":{"days":9,"location":"Lambourn","seaonWinners":119,"seasonStrikeRate":27,"stake":-69.77,"trainerId":2,"trainerName":"Nicky Henderson"},"trainerName":"Nicky Henderson"}},
这是我在 Angular 上尝试过的,但它没有显示任何内容。非常感谢任何帮助,谢谢。
var horseApp = angular.module('horseApp', []);
horseApp.controller('HorseCtrl', function ($scope, $http){
$http.get('http://localhost:8080/restful-services/api/getAllHorses').success(function(data) {
$scope.horse = data;
});
});
<body ng-controller="HorseCtrl">
<h2>Horses</h2>
<table>
<tr>
<th>Age</th>
<th>Name</th>
<th>Breeder</th>
</tr>
<tr ng-repeat="age in Horse.age ">
<td>{{horse.age}}</td>
<td>{{horse.horesName}}</td>
<td>{{horse.breeder}}</td>
</tr>
</table>
</body>
最佳答案
$http.success
已过时,请使用then
代替。请参阅$http- 您想要迭代您的数组,因此修改 ng-repeat 应该可以做到这一点,现在您指向一个无效的属性 Age,因为
horse
是一个数组。horse
的更好名称是horses
。
修改后的代码
horseApp.controller('HorseCtrl', function ($scope, $http){
$http.get('http://localhost:8080/restful-services/api/getAllHorses')
.then(function(response) {
$scope.horse = response.data;
});
});
HTML
<tr ng-repeat="item in horse">
<td>{{item.age}}</td>
<td>{{item.horesName}}</td>
<td>{{item.breeder}}</td>
</tr>
关于angularjs - 使用 REST 进行 Angular http 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976770/