Question

I have a problem with a service angularjs , do not make more than one http request .

The service :

.factory('checkpoints', function($http, user_auth) {
            var promise = $http({
                method: "POST",
                url: remote_ws + 'index/',
                data: user_auth.get,
                cache:false
            }).
                    success(function(data, status, headers, config) {
                        if (data.result == "OK") {
                            window.localStorage.setItem("last_id", data.update[0].last_id);
                            window.localStorage.setItem("last_count", data.update[0].last_count);
                        }
                        return data;
                    }).
                    error(function(data, status, headers, config) {
                        data.result = "ERROR";
                        data.status = status;
                        return data;
                    });
            return promise;
        })

In the controller:

var UpdateCheckpoints = function() {
        checkpoints.then(function(promise) {
            if (promise.data.result == "OK") {
                $scope.map.checkpoints = promise.data.markers;
                _.each($scope.map.checkpoints, function(marker) {
                    marker.distance = $scope.c_distance(marker);
                    marker.onClicked = function() {
                        onMarkerClicked(marker.id);
                    };
                });
            } else {
                $location.search({error: true, error_text: session_error}).path("/login");
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
            }
        })

When I call : UpdateCheckpoints ( ), the result is null.

Is done only the first request.

It is a problem with $http or statement of service?

Was it helpful?

Solution

From the documentation:

Angular services are singletons objects or functions ...

The purpose of the service factory function is to generate the single object, or function, that represents the service to the rest of the application.

Try changing your factory to something along these lines:

app.factory('checkpointService', function($http, user_auth) {

  return {
    getCheckpoints: function() {

      return $http({
          method: "POST",
          url: remote_ws + 'index/',
          data: user_auth.get,
          cache: false
        })
        .success(function(data, status, headers, config) {
          if (data.result == "OK") {
            window.localStorage.setItem("last_id", data.update[0].last_id);
            window.localStorage.setItem("last_count", data.update[0].last_count);
          }
          return data;
        })
        .error(function(data, status, headers, config) {
          data.result = "ERROR";
          data.status = status;
          return data;
        });
    }
  };
});

And call it with:

checkpointService.getCheckpoints().then(function(promise) { ...

Now the checkpointService will be a singleton object, but everytime you call getCheckpoints a new call via the $http service should be made.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top