0

I try to throw error modal window when the gps not enable. perhaps it's enabled then it will call servcie callback using $http.post method. But without gps function it's working and return promises fine. if use GPS method above the post method then i got some error like "Uncaught TypeError: Cannot read property 'then' of undefined" What's wrong with my code. Anyone can give me some idea..

服務工廠工廠使用GPS探測器函數中:

.service('DataServiceFactory', function($http, $rootScope){   //dynamic $http JSON Data Passing service 
      return{ 
       serviceDataReq : function(reqUrl, reqData){ 
        cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){ 
         if(enabled == 1){ 
          return $http.post(reqUrl, reqData); 
         }else{ 
          $rootScope.ErrorMsg = "Please Turn On your GPS location.."; 
          $('#ErrorModal').modal('show'); 
         } 
        }, function(error){ 
         $rootScope.ErrorMsg = "GPS location Error : "+error; 
         $('#ErrorModal').modal('show'); 
        }) 
       } 
      } 
     }) 

在控制器:

DataServiceFactory.serviceDataReq($rootScope.primaryLiveServiceApiUrl, angular.toJson($scope.LoginAuthReq)).then(function(response) { 

}, function(error){ 

}); 
+0

你可以添加一個'返回{'聲明serviceDataReq後':功能(reqUrl,reqData){',看看它是否起作用? – user3632710

+0

未捕獲的SyntaxError:意外的令牌。在GPS功能 –

回答

1

我現在沒有cordova的例子來測試你的代碼,但看着它,我會說,如果設備沒有GPS功能ilities,你的服務方法沒有回覆承諾。

我會做這樣的事情:

.service('DataServiceFactory', function($q, $http, $rootScope){   //dynamic $http JSON Data Passing service 
     return { 
      serviceDataReq: function(reqUrl, reqData){ 
       var deferred = $q.defer(); 
       cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){ 
        if(enabled == 1){ 
         deferred.resolve($http.post(reqUrl, reqData)); 
        }else{ 
         $rootScope.loaded = false; 
         $rootScope.ErrorMsg = "Please Turn On your GPS location.."; 
         $('#ErrorModal').modal('show'); 
         deferred.reject("Please Turn On your GPS location.."); 
        } 
       }, function(error){ 
        $rootScope.ErrorMsg = "GPS location Error : "+error; 
        $('#ErrorModal').modal('show'); 
        deferred.reject("GPS location Error : "+error); 
       }); 
       return deferred.promise; 
      } 
      }; 
    }) 
+0

我得到了服務器的響應,如果GPS啓用其他模式窗口似乎。但現在的問題是我無法獲得$ http.post回調承諾使用.then()從控制器 –

相關問題