numediaweb
numediaweb

Reputation: 17010

Angularjs; use $http in service returns reference instead of actual data

I'm using the services directive in Angularjs not factory and I need to populate a json file to local variable;

/* Contains projects on the town */
leMaireServicess.service('cityService', function($http) {

    // JSON regions and cities loader
    this.cities = [];

    // initCities 
    this.initCities = function() {
        this.cities = $http.get('data/census/cities.js').success(function(data) {
            return data;
        });
        return this.cities;
    };

    // Get city info
    this.getCity = function() {
        return this.cities;
    };
});

And in my controller I have

// Saved game controller
leMaireControllers.controller('GameCoreCtrl', function($scope, cityService) {

    /* Control the town project slides */
    cityService.initCities();
    $scope.city = cityService.getCity();

    console.log($scope.city);
});

But instead of returning the actual data, it returns;

Object {then: function, catch: function, finally: function, success: function, error: function}

Upvotes: 2

Views: 5116

Answers (3)

Thomas Schultz
Thomas Schultz

Reputation: 2467

$http returns a promise which is what you're setting this.cities to.

This might help explain more, https://stackoverflow.com/a/12513509/89702

In your controller you should be able to do something like this...

cityService.initCity().then(function(data) { $scope.city = data; }

Upvotes: 3

zszep
zszep

Reputation: 4458

You can use a watch to make this work (see plunker)

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope,cityService) {
  //$scope.cities = [];
  $scope.service = cityService;
  cityService.initCities();


  $scope.$watch('service.getCity()', function(newVal) {
    $scope.cities = newVal;
    console.log(newVal)
  });


});

app.service('cityService', function($http) {
  var that = this;
  this.cities = [];

  this.initCities = function() {
      $http.get('data.js').success(function(data) {
          that.cities = data.cities;
      });
  };

  this.getCity = function() {
      return this.cities;
  };
});

Upvotes: 6

dimirc
dimirc

Reputation: 6605

You are working with promises which represent the result of an action that is performed asynchronously. Try it this way:

leMaireServicess.service('cityService', function($http) {

    this.promise = {};

    // initCities 
    this.initCities = function() {
        this.promise = $http.get('data/census/cities.js');
    };

    // Get city info
    this.getCity = function() {
        return this.promise;
    };
});

And in the controller you need to put your code in a callback:

// Saved game controller
leMaireControllers.controller('GameCoreCtrl', function($scope, cityService) {

    /* Control the town project slides */
    cityService.initCities();
    cityService.getCity().then(function(result){
        $scope.city = result.data;
        console.log($scope.city);
    });

});

Upvotes: 1

Related Questions