2016-05-16 49 views
0

我想使用rootScope全球,這樣同樣可以在控制器中獲取。無法使用rootScope內window.onnotification

app.js:

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives']) 
.run(function($ionicPlatform,$rootScope) { 
    $ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     cordova.plugins.Keyboard.disableScroll(true); 
    } 
    if (window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleDefault(); 
    } 

    pushNotification = window.plugins.pushNotification; 
    pushNotification.register(
    onNotification, 
    errorHandler, 
    { 
    'badge': 'true', 
    'sound': 'true', 
    'alert': 'true', 
    'ecb': 'onNotification', 
    'senderID': '999999999999', 
    } 
    ); 


    }); 
}) 

window.onNotification = function(e){ 

     switch(e.event){ 
     case 'registered': 
      if(e.regid.length > 0){ 

      var device_token = e.regid; 

       alert('registered :'+device_token); 
       $rootScope.devicetoken = device_token; 

      } 
     break; 

     case 'message': 
      alert('msg received: ' + e.message); 
      break; 

     case 'error': 
      alert('error occured'); 
     break; 

     } 
}; 

window.errorHandler = function(error){ 
    alert('an error occured'); 
} 

我越來越device_token和警報獲得。但它不會在rootScope內部使用它在控制器中。

Controller.js:

angular.module('app.controllers', []) 

.controller('onWalletWelcomesCtrl', function($scope, $ionicModal,User,$ionicLoading,$rootScope) { 

    $ionicModal.fromTemplateUrl('signup-modal.html', { 
     id: '1', // We need to use and ID to identify the modal that is firing the event! 
     scope: $scope, 
     backdropClickToClose: false, 
     animation: 'slide-in-up' 
    }).then(function(modal) { 
     $scope.oModal1 = modal; 
    }); 

    $scope.proceed = function(){ 
     alert($rootScope.devicetoken); 
     $ionicLoading.show({template: '<ion-spinner icon="android"></ion-spinner>'}); 

    } 

}) 

同時提醒在進入功能,我越來越不確定。 我應該如何在window.onNotification中使用rootScope。我的主要目的是將devicetoken傳遞給控制器​​。請讓我分享這些變數的最佳做法。

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives']) 
.run(function($ionicPlatform,$rootScope) { 
    $ionicPlatform.ready(function() { 

    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     cordova.plugins.Keyboard.disableScroll(true); 
    } 
    if (window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleDefault(); 
    } 

    pushNotification = window.plugins.pushNotification; 
    pushNotification.register(
    onNotification, 
    errorHandler, 
    { 
    'badge': 'true', 
    'sound': 'true', 
    'alert': 'true', 
    'ecb': 'onNotification', 
    'senderID': '9999999999', 
    } 
    ); 

    }); 
}) 

window.onNotification = function(e){ 

     switch(e.event){ 
     case 'registered': 
      if(e.regid.length > 0){ 

      var device_token = e.regid; 

      alert('registered :'+device_token); 
      $rootScope.devicetoken = "hi"; 
      $scope.$apply(); 

      } 
     break; 

     case 'message': 
      alert('msg received: ' + e.message); 

     break; 

     case 'error': 
      alert('error occured'); 
     break; 

     } 
}; 

window.errorHandler = function(error){ 
    alert('an error occured'); 
} 

但我在控制器中發出警報時未定義。

+0

嘗試定義$ rootScope.devicetoken = ''; – mtamma

回答

-1

嘗試定義$ rootScope.devicetoken上運行範圍

.run(function($ionicPlatform,$rootScope) { 
    $rootScope.devicetoken = ''; 

確保window.onNotification執行之前$ scope.proceed

+0

$ rootScope是一個全局的,角度提供的對象。你不應該自己初始化它,即使你做了,它也會是一個對象。 – matmo

+0

謝謝,更新代碼:) – mtamma

0

您需要將您的.run(function($ionicPlatform,$rootScope) {...}塊內的window.onNotification = function(e){..}聲明。

$ rootScope將在您的onNotification處理程序中未定義,您現在已放置它 - 您需要在run()塊內聲明您的onNotification處理程序,以便在定義它時可以訪問rootScope對象。

此外,因爲您將更新與生活的角度生命週期之外的事件處理程序的rootScope(角不知道它),你需要調用引發新手動消化週期。在您的通知處理程序,你需要用一個$ rootScope.apply()你$rootScope.devicetoken = device_token;線,使得它看起來像這樣:

$rootScope.apply(function(){ 
     $rootScope.devicetoken = device_token; 
}); 
+0

沒有運氣。我試圖保持onNotification內,但它拋出未引用錯誤 – kpvsrkp

+0

如果您將它插入到運行塊,那麼你將有機會獲得一個有效的$ rootScope對象。如果你這樣做,那麼其他東西是不確定的。你嘗試過調試嗎? – matmo

0

您正在使用$ rootScope在window.onNotification所以它不是角範圍內,所以因爲你需要告訴角度做更新。 所以你需要後$rootScope更新用添加$scope.$apply();

+0

這就是......正是我所說的...... – matmo

+0

你能提供任何鏈接來查看和運行源代碼嗎? –

+0

我試了$ scope。$ apply()和$ rootScope。$ apply()。但沒有運氣。在警報控制器內部時仍然未定義 – kpvsrkp