Uczę się AngularJS i mam skonfigurowaną strukturę projektu, ale kiedy zadzwonię do API, który zwraca mi JSON, nie mogę tego wyświetlić w html.AngularJS Display JSON Data
Pomysł polega na kliknięciu przycisku, a zwracany wynik zostanie wyświetlony w {{answer}}.
HTML:
<div ng-app="xileapp">
<div ng-controller="searchController">
<input type="button" ng-click="search()" value="search" />
<div>Answer: {{answer}}</div>
</div>
</div>
Kontroler:
xile.controller('searchController', ['personSearch', '$scope', function (personSearch, $scope) {
$scope.search = function() {
$scope.answer = personSearch.findPlayer();
}
}]);
Usługa:
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 trafia sukces z poprawną odpowiedzią. Jak mogę teraz uzyskać dane do wyświetlenia w kodzie HTML.
findPlayer zwraca nic, trzeba wartość –