离子。使用$ http给出错误无法读取undefined的属性'protocol'

时间:2016-08-24 12:51:57

标签: angularjs ionic-framework

此问题与another one有关。 在我添加$ ionicPlatform之前,我的服务运行得很好,但现在$ http出现了问题。 以下是注射剂的示例:

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

使用$ http和$ 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 个答案:

答案 0 :(得分:1)

你需要发回承诺,做return response.data是行不通的。

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;

在您使用此服务的地方:

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

如果您需要有关使用$ q的更多说明,请与我们联系;我很乐意提供更多细节。