2015-07-21 58 views
0

嘗試使用android在phonegap中創建android應用程序。

angular.module('myModule').config(['$rootScope', function($rootScope) { 
    $rootScope.internet = { status: 0, type: 'none'}; 
    document.addEventListener("offline", function(){ 
     $rootScope.internet.status = 0; 
     $rootScope.internet.type = navigator.connection.type; 
    }, false); 
    document.addEventListener("online", function(){ 
     $rootScope.internet.status = 1; 
     $rootScope.internet.type = navigator.connection.type; 
    }, false); 
    }]); 

我只想在navigator.connection更改或進行聯機/脫機時更改全局可變參數。

或者我該如何將watch綁定到此navigator.connection全局變量。

回答

0

不能使用$rootScope角應用程序的配置階段裏面,因爲它沒有在它運行之後.config塊結束run塊被創建。

它可以從角度run階段獲得。您可以將這段代碼放入運行階段。

您可以在每個摘要上使用函數並返回navigator.connection值。

代碼

angular.module('myModule').run(['$rootScope', function($rootScope) { 
    $rootScope.internet = { status: 0, type: 'none'}; 
    document.addEventListener("offline", function(){ 
     $rootScope.internet.status = 0; 
     $rootScope.internet.type = navigator.connection.type; 
    }, false); 
    document.addEventListener("online", function(){ 
     $rootScope.internet.status = 1; 
     $rootScope.internet.type = navigator.connection.type; 
    }, false); 
    $rootScope.$watch(function(){ 
     return navigator.connection; 
    }, function((newVal, oldVal){ 
     console.log(newVal) 
    }) 
}]); 
+0

@ubombi它能幫你嗎? –