0
I want to learn how to recover the data from a function that does a GET in an API and returns me the JSON. I want to recover the data and fill in a list... with the ng-repeat
.
Is going to come Undefined and then I don’t even know what to put on ng-repeat
.
Angular JS
/* Factory */
App.factory('dealerFactory', ['$http', function($http) {
var factory = {
obterDados: _obterDados
}
// Lista de Concessionárias
function _obterDados() {
$http.get("http://localhost:8888/dealers").then(
function(response){
return response.data;
}
);
}
return factory;
}]);
App.controller('ListaCtrl', ['$scope', 'dealerFactory', function($scope, dealerFactory){
$scope.dealers = [];
$scope.dealers = dealerFactory.obterDados();
}]);
HTML
<ion-content ng-controller="ListaCtrl">
<ion-list>
<ion-item ng-repeat="{{ result in dealers }}" id="{{result.id}}">
<h3>{{result.dealer}}</h3>
<h4>{{result.stars}}</h4>
</ion-item>
</ion-list>
</ion-content>
Updating:
When I do the code below just for debug reasons, it returns Undefined. The URL is right, it is bringing json. I tested the LINK in the browser and also gave console.log
in function Return and brought me the data.
$scope.dealers = [];
$scope.dealers = dealerFactory.obterDados();
console.log($scope.dealers);
I believe it is something related to Asynchronization. The variable has not changed yet and the Return is executed first... I don’t know how to fix.
Thanks, it did. It’s coming empty and there’s no error in the code. I downloaded a Chrome plugin that hides the error of Allow Control Access Origin... So... I use Ionic Server on port 8100 and Apigility on port 8888. So it’s like another.
– Diego Souza
Are you using CORS on your Ionic Server? Because I had problems also using Ionic on the localhost and my API on another domain, I had to implement in my back-end a treatment to accept any source, at least while I was in the development phase
– Guilherme Sanches
I’m not, I don’t know how to configure it. Beyond what I’m using Apigility to build API’s.
– Diego Souza
But now it’s stopped making the mistake. It’s giving this:
[$parse:syntax] Syntax Error: Token 'in' is an unexpected token at column 8 of the expression [dealer in dealers] starting at [in dealers]
– Diego Souza
I’ve arranged the code, check it now, in {{dealers in dealer}} there’s no interpolation... it’s just double quotes
– Guilherme Sanches
Oh I got it. Now it really stopped the error. I’m picking up the tricks, but the Allow error returned... I’ll search how to fix.
– Diego Souza
@Zoom see this link https://apigility.org/documentation/recipes/allowing-request-from-other-domains has a good explanation of how to accept requests from other sources in Apigility
– Guilherme Sanches
I’m doing just that. Hehe’s installing...
– Diego Souza
Ready. I configured, but CONNECTION REFUSED is giving. There are some parameters, I think I can not fill. I will edit the question and post.
– Diego Souza
Dude, I got it set up. Now the problem is you’re not listing. This appears in HTML
<!-- ngRepeat: dealer in dealers -->
– Diego Souza
I updated my question.
– Diego Souza
Can you understand my question ?
– Diego Souza