[英]Http request using AngularJS controller
我試圖顯示來自服務器的數據,我試圖制造一個控制器並將響應復制到scope屬性。 我嘗試了“ response”和“ response.data”,但它們都沒有起作用。
html文件如下所示:
<!DOCTYPE html>
<html>
<head>
<script src="angular.min.js"></script>
<title>Service Call ...</title>
</head>
<body>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("https://linkToServer.json")
.success(function(response) {$scope.trainings = response.data;
});
});
</script>
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat="tr in trainings">
{{ tr.id }}
</li>
</ul>
</div>
</body>
</html>
json結構類似於:
[
{
"id" : "sm1001",
"name" : "Lu",
},
{
"id" : "lm9898",
"name" : "Di",
},
....
]
怎么了?
問題的Plunkr
當我更正json時,您的朋克工作了。 您沒有在示例中包含json。 我從您的問題中復制了它。
"name" : "Lu",
需要是
"name" : "Lu"
http://plnkr.co/edit/toSicojh58y1lWrziyI8?p=preview
這可能只是您的示例數據。 角度代碼工作正常。
我認為這是因為您的html全部搞砸了。 首先是div
,然后是head
標簽,我已經更新了您的plunkr,現在可以正常使用了: http ://plnkr.co/edit/6Y1dX9vKkDANdZyEd0aj?p=preview
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.