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

Deadly 提交于 2019-12-22 18:25:03

问题


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}

回答1:


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



回答2:


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




回答3:


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

});


来源:https://stackoverflow.com/questions/21487413/angularjs-use-http-in-service-returns-reference-instead-of-actual-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!