Return JSON Function Data and Populate DIV

Asked

Viewed 1,024 times

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.

1 answer

0


Well, first create the variable in the angular scope,

App.controller('ListaCtrl', ['$scope', 'dealerFactory', 
 function($scope,    dealerFactory){

$scope.dealers = [];
$scope.dealers = dealerFactory.obterDados();

// não precisa fazer mais nada
}]);

HTML

<ion-content ng-controller="ListaCtrl">
//correção nova aqui
 <ion-list ng-repeat="dealer in dealers">
   <ion-item id="{{dealer.id}}">
      <h3>{{dealer.dealer}}</h3>
      <h4>{{dealer.stars}}</h4>
   </ion-item>
 </ion-list>
</ion-content>

CORRECTION:

 App.factory('dealerFactory', ['$http', function($http) {
   var factory = {
    obterDados: _obterDados
   }

function _obterDados() {
   $http.get("http://localhost:8888/dealers/1").then(
     function(data){
//como voce usou o then e não o sucess o data que tem o json esta dentro de
//data.data e não somente data
//se voce usa o .sucess ao invés do .then, usa apenas data no retorno
        return data.data;
      }
   );
 }

   return factory;
}]);

I believe this solves your problem, now you need to see if your API is actually returning the data, because you said that sometimes it returns Undefined, maybe the API is having some problem.

Anything please let me know,

I hope I’ve helped

Hugs!

  • 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.

  • 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

  • I’m not, I don’t know how to configure it. Beyond what I’m using Apigility to build API’s.

  • 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]

  • I’ve arranged the code, check it now, in {{dealers in dealer}} there’s no interpolation... it’s just double quotes

  • 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.

  • @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

  • I’m doing just that. Hehe’s installing...

  • 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.

  • Dude, I got it set up. Now the problem is you’re not listing. This appears in HTML <!-- ngRepeat: dealer in dealers -->

  • I updated my question.

  • Can you understand my question ?

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.