2

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}
numediaweb
  • 16,362
  • 12
  • 74
  • 110

3 Answers3

6

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;
  };
});
zszep
  • 4,450
  • 4
  • 38
  • 58
3

$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; }

Community
  • 1
  • 1
Thomas Schultz
  • 2,446
  • 3
  • 25
  • 36
  • Thanks for the quick answer. But how can I access the retrieved data inside the service itself? Is it the same way as if inside the controller? – numediaweb Jan 31 '14 at 18:50
  • 1
    Just to correct your solution, you mean `cityService.initCities()` instead of `cityService.getCity()`. – numediaweb Jan 31 '14 at 18:56
1

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);
    });

});
dimirc
  • 6,347
  • 3
  • 23
  • 34