我正在創建一個單一功能:使用AngularJS單擊按鈕時,我對來自服務器的一些JSON數據進行ajax調用。AngularJS Ajax調用
沒有按鈕功能下面的代碼工作:
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="customersController">
<ul>
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
</div>
<script>
function customersController($scope,$http) {
$http.get("http://www.w3schools.com//website/Customers_JSON.php")
.success(function(response) {$scope.names = response;});
}
</script>
</body>
</html>
但是當我添加一個按鈕,點擊功能數據犯規得到顯示該按鈕的點擊
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="customersController">
<button ng-click="getData()">Get Data</button>
<ul>
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
</div>
<script>
function customersController($scope, $http) {
$scope.getData()= $http.get("http://www.w3schools.com//website/Customers_JSON.php")
.success(function (response) { $scope.names = response; });
}
</script>
</body>
</html>
請幫忙!
'$ scope.getData()= $ http.get(...)'。這不是有效的JavaScript代碼。 – dfsq 2015-02-09 10:02:16
感謝您的回覆! 但是也嘗試過您的代碼,沒有從點擊按鈕獲取任何數據! – 2015-02-09 10:12:02