2017-03-01 79 views
1

我在使用Angular 1.3的插件https://github.com/phonegap-build/PushPlugin/,我需要在收到「registered」事件時將regid發送到服務器。 問題是我沒有$ http對象在此上下文中調用我的服務器。我該如何做到這一點?

function onNotification(e){ 
    if(e.event == "registered"){ 
     var req = { 
      method: "POST", 
      url: "http://myurl.com/?var="+e.regid 
     }; 
     $http(req).success(function(data){ 
      alert(data); 
     }); 
    } 
} 
+0

您可以發佈'onNotification'功能的父零件? –

回答

0

變化$http電話如下,.success已被棄用。

$http({ 
    method: "POST", 
    url: "http://myurl.com/?var="+e.regid 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
    alert(response); 
}, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
}); 

Ref。 :https://docs.angularjs.org/api/ng/service/ $ http

此致。

+0

由於onNotification方法不在任何控制器,服務或工廠內,當我調用$ http我收到錯誤「$ http未定義」。 –

1

我剛學會注入的$ HTTP到事件方法:

$http = angular.injector(["ng"]).get("$http");