0

I am experiencing the same issue as posted in a recent topic - 30702965 Accessing objects returned from a factory. In fact I have followed the advice in that response but still do not get any results in the view. As in that previous post I have created a factory:

myapp.factory("eventsService", function($http) {

var events = [];
return {
getEvents: function() {
  return $http.get('data/events.php').then(function(response) {
    events = response;
    // console.log(response);
    return events;
  });
 }};
});

This returns the promise which is seen in the console.log.

I coded the controller as per the advice in the response as follows:

myapp.controller('eventsController', function($scope, eventsService) {
$scope.events = [];
eventsService.getEvents().then(function(data){
  $scope.events = data;
  console.log(data);
});

});

Again, the console.log shows that I have got the object:

object {data: Array[2], status: 200, config: Object, statusText: "OK"}

All good so far, but I still don't get anything in the view:

<ion-content ng-controller="eventsController">

    <ion-item ng-repeat="event in events">{{ event.title }} </ion-item>

  </ion-content>

I'm lost as how to display the events. What am I missing?

Oh, by the way although this is a php backend going to a mysql database it is working. In fact if I don't use a service and code the http get directly into the controller it all works fine and I see the results as expected.

Please help this is driving me nuts!!

boony
  • 87
  • 1
  • 9
  • 1
    use `$scope.events = data.data;` – shakib Jul 09 '15 at 07:30
  • omg!! That's it. But I don't understand and I certainly don't see this in any of the tutorials etc. What is this doing?? – boony Jul 09 '15 at 10:57
  • `$http` has both `then` and `success, error` method for callback handling. the method parameters for these two are different. that is why `data` contains the response data and status, statustext and config data also. the difference is mentioned here http://stackoverflow.com/a/16387215/728563 – shakib Jul 10 '15 at 08:43

0 Answers0