0

This question is related to another one. Before I did added $ionicPlatform, my service working just fine, but now there is something wrong with $http. Here is example of injectables:

(function () {
    "use strict";
    angular.module('service', ['ionic'])
   .service('BBNService', ["$http", "$localStorage", "$ionicPlatform",
       function ($http, $localStorage, $ionicPlatform) {

And using of $http and $ionicPlatform

this.tips = function () {
            var url;
            $ionicPlatform.ready(function () {
                if (window.Connection) {
                    if (navigator.connection.type == Connection.CELL_4G || navigator.connection.type == Connection.WIFI) {
                        if (this.getDayId = 0)//If Sunday - retrieve updated tips
                            url = this.host + "/tips/";
                        else
                            url = "data/tips.json";//If not - use saved data
                    }
                }
            });
            var request = $http({
                method: "GET",
                url: url
            }).then(
                function mySucces(response) {
                    return response.data;
                },
                function myError(response) {
                    return response.data;
                });
            return request;
        };

1 Answer 1

1

You need to send back the promise, doing a return response.data is not gonna work.

var deferred = $q.defer();
var request = $http({
                method: "GET",
                url: url
            }).then(
                function mySucces(response) {
                     deferred.resolve(response.data);
                },
                function myError(response) {
                    deferred.reject(response.data);
                });

   return deferred.promise;

And at the place where you consume this service:

BBNService.tips().then(
      function(data) { //success call back with data  },
      function(data) { //error call back with data }
           );

Please let me know if you need more explanation on using $q; always happy to give more details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.