0

Im beginning to work with Ionic and I am having problems populating the list below using this code. I think that when the promise returns the list to the scope the screen has already started to load so nothing shows on the screen. As per my understanding whatever is implemented in the .then() only happens after the promise returns the data. When debugging this via chrome debugger the $scope.clients never recieves the data. what should I change in this approach for the list to load correctly? Could it be because I'm calling .getClients in the controller's body? Any tip on what is wrong with my approach is much appreciated.

.factory('ClientFactory', function($http){
  //restful api
  var urlBase = "http://localhost:8080/client";
  return{
    all: function(){
      return [
          { name: 'Client 1', id: 1 },
          { name: 'Client 2', id: 2 },
          { name: 'Client 3', id: 3 },
          { name: 'Client 4', id: 4 },
          { name: 'Client 5', id: 5 }
      ];
    },
    getClients: function(){

      var listaClientes = $http.get(urlBase).
        then(function(response, listaClientes) {
          var x = 'feitoooo';
          console.log(response.data);
          listaClientes = response.data;
          return listaClientes;
        // this callback will be called asynchronously
        // when the response is available
        }, function(response) {
          listaClientes = 'void';
          //return listaClientes;
          // called asynchronously if an error occurs
        // or server returns response with an error status.
      });


    }
  }

})


.controller('ClientCtrl', function($scope, $http, ClientFactory, $timeout, $ionicModal) {

  $scope.clients = ClientFactory.getClients();

  $scope.newClient = function(){ 
     console.log('newClient chamada!!');      

     $scope.clientData = {};

      // Create the login modal that we will use later
      $ionicModal.fromTemplateUrl('templates/client-add.html', {
        scope: $scope
      }).then(function(modal) {
        $scope.modal = modal;
        $scope.modal.show();
  });

      // Triggered in the login modal to close it


}


<ion-view view-title="Client">
  <ion-content>
    <h1>Client control</h1>

    <div class="buttons">
      <button class="button button-icon ion-plus" ng-click="newClient()"></button>
    </div>

    <ion-list>
      <ion-item ng-repeat="client in clients" href="">
        {{client.name}}
      </ion-item>
    </ion-list>

  </ion-content>
</ion-view>
wdoering
  • 321
  • 6
  • 15
  • see this : http://stackoverflow.com/questions/32529045/ionic-return-factory-from-data-from-json-file/32529357#32529357 – Mudasser Ajaz Sep 12 '15 at 18:52
  • Sorry for duplicating, this was sort of a hard question to ask. This worked: //factory getClients: function(){ return $http.get(urlBase); } //controller: ClientFactory.getClients().then(function(response){ $scope.clients = response.data; }); – wdoering Sep 14 '15 at 19:40

0 Answers0