2015-06-16 76 views
-1

這裏從API的REST的數據是我執行如何獲得角的js

<!DOCTYPE html ng-app="myApp"> 
<html> 
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
<body> 
<div ng-controller="customersCtrl"> 
    <ul> 
     <li ng-repeat="x in patients"> 
      {{ x.area }} 
     </li> 
    </ul> 
</div> 
<script> 
var app = angular.module('myApp', []); 
app.controller('customersCtrl', function($scope, $http) { 
$http.get("http://localhost:8080/MedZHealth/rest/patient/12") 
.success(function (response) {$scope.patients = response.addressList;}); 
}); 
</script> 
</body> 
</html> 

這裏的代碼是JSON:

{ 
    "addressList":[{ 
    "area":"NE1 WX", 
    "city":"London", 
    "pin":"0", 
    "street":"221B Bekar Street" 
    }, 
    { 
    "area":"Manorayan Palya", 
    "city":"Bangalore", 
    "pin":"560032", 
    "state":"Karnatak", 
    "street":"11th Cross" 
    }] 
} 

爲什麼不工作?

+2

抱歉,您需要更具體地瞭解發生了什麼錯誤。是否有錯誤訊息?你的錯誤信息是什麼?當你嘗試做什麼時會發生什麼? –

+0

它必須是response.data.addressList – nikhil

+0

你的錯誤/問題是什麼? –

回答

-1

@PriyamChakraborty

這是我的例子工程:

//Controller 
angular.module('testprojektApp') 
    .controller('CityCtrl', ['$log', '$scope', '$http', function ($log, $scope, $http) { 
    $http.get('/data/country.json') 
    .success(function (data) { 
     $scope.addresses = data; 
     $log.info('Adresslist:', $scope.addresses.addressList); 
    }) 
    .error(function (data, status) { 
     $log.error('Status:', status); 
    }); 
    }]); 

//查看:

<div ng-repeat="address in addresses.addressList"> 
<ul> 
    <li>{{address.city}}, {{address.street}}, {{address.zip}}, {{address.area}}</li> 
</ul> 
</div> 
+0

@PriyamChakraborty我修改了我的答案,請試試。 – yuro

0

您可以嘗試添加 '$範圍' 和 '$ HTTP' 作爲依賴到控制器,如下圖所示

app.controller('customersCtrl', ['$scope', '$http', function($scope, $http) { 
    $http.get("http://localhost:8080/MedZHealth/rest/patient/12") 
     .success(function (response) { 
      $scope.patients = response.addressList; 
     }); 
}]); 

此外,只有當後端與服務器位於同一服務器上時,該功能纔有效。如果後端位於不同服務器上,則應寫入機器的IP地址而不是「localhost」。