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}

Answers


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

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


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

});

Need Your Help

Serve a minimise prototype.js file with Google AJAX Libraries API?

prototypejs cdn minimize

Is Google AJAX Libraries API offering a way to serve framework file in minimize way?

Flickering when using mouse in and mouse out

javascript

I am trying to create an effect where when mouse is moved over the image it should display a translucent black box on the image and display some details on top. These div's contain images and the p...