How to start http call as background in ionic structure?

I use an ionic structure. I have several HTTP services that work fine. Now the problem is that whenever I get a response to any HTTP call. I can’t move on.

Is it possible to start the HTTP service as a background process. Therefore, my application continues to work without waiting for the result.

here is my code

articleService.getArticles().then(function() {

    },function(err){

    });

and sercvice code

$http({
                url: "http://myservice.com",
                data: { user_id: 1 },
                method: 'POST',
                withCredentials: true,
            }).success(function (data) {
                deferred.resolve(data);
            }).error(function (err) {
             deferred.resolve(0);
            })
            return deferred.promise;
        }

Any idea? Do I need a solution in the ionic structure that will work for both ios and andriod?

thank

+4
source share
3 answers
+1

this plunker, , , , .

var app = angular.module('angularjs-starter', []);

app.config(function($routeProvider) {
    $routeProvider.
      when('/', {controller:'StartCtrl', templateUrl:'start.html'}).
      when('/main', {controller:'MainCtrl', templateUrl:'main.html'}).
      otherwise({redirectTo:'/'});
});

app.controller('MainCtrl', function($scope, Poller) {
  $scope.name = 'World';
  $scope.data = Poller.data;
});
app.controller('StartCtrl',function(){});
app.run(function(Poller) {});

app.factory('Poller', function($http, $timeout) {
  var data = { response: {}, calls: 0 };
  var poller = function() {
    $http.get('data.json').then(function(r) {
      data.response = r.data;
      data.calls++;
      $timeout(poller, 1000);
    });

  };
  poller();

  return {
    data: data
  };
});
+1

I may have misunderstood your question, but I think your service code is incorrect.

Try something like this

myApp.factory('articleService', function($http) {
    return {
        getArticles: function getArticles() {
              return $http({...}); // $http returns a promise, so you dont need your own defer.promise
        }
    }
});


//usage
//first: send or get data async
articleService.getArticles().then(function(resp){
    alert('called second');
    ...
});
// second: do something else, this will not wait for your response
alert('called first');
0
source

All Articles