Tôi đang học AngularJS và có cấu trúc của dự án được thiết lập nhưng khi tôi gọi API trả về JSON, tôi không thể hiển thị nó trong html.AngularJS Hiển thị dữ liệu JSON
Ý tưởng là bạn nhấp vào nút và kết quả được trả lại sẽ được hiển thị trong {{answer}}.
HTML:
<div ng-app="xileapp">
<div ng-controller="searchController">
<input type="button" ng-click="search()" value="search" />
<div>Answer: {{answer}}</div>
</div>
</div>
Bộ điều khiển:
xile.controller('searchController', ['personSearch', '$scope', function (personSearch, $scope) {
$scope.search = function() {
$scope.answer = personSearch.findPlayer();
}
}]);
dịch vụ:
xile.service('personSearch', function ($http) {
this.findPlayer = function() {
$http({
method: 'GET',
url: 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/Crucify?api_key=222015c4-0898-4f6b-a7d5-2a23c3e0344d'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
return response;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
return response;
});
};
});
URL được đánh thành công với câu trả lời đúng. Làm thế nào bây giờ tôi có được dữ liệu để hiển thị trong HTML.
findPlayer trả về không có gì, bạn cần phải trả lại giá trị –