2016-05-16 6 views
0

Ich versuche, rootScope als global zu verwenden, damit dasselbe im Controller abgerufen werden kann.rootScope kann nicht innerhalb von window.onnotification verwendet werden

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'); 
} 

Ich erhalte device_token und in Alarm zu bekommen. aber es geht nicht in rootScope, um es im Controller zu verwenden.

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>'}); 

    } 

}) 

Ich bin nicht definiert zu werden, während Funktion Alarmierung in fortzufahren. Wie verwende ich rootScope in window.onNotification. Meine Hauptabsicht besteht darin, die Devicetoken an den Controller zu übergeben. Bitte lassen Sie mich die beste Praxis, um die Variablen zu teilen.

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'); 
} 

Noch werde ich undefiniert während der Alarmierung im Controller.

+0

Versuchen Sie, $ rootScope.devicetoken = '' zu definieren; – mtamma

Antwort

-1

Try $ rootScope.devicetoken auf der Flucht Umfang

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

sicher zu definieren, dass window.onNotification vor scope.proceed

+0

$ rootScope ist ein globales, eckig bereitgestelltes Objekt. Sie sollten es nicht selbst initialisieren müssen, und selbst wenn, wäre es ein Objekt. – matmo

+0

danke, update den Code :) – mtamma

0

$ ausgeführt Sie benötigen window.onNotification = function(e){..} Erklärung innerhalb Ihres .run(function($ionicPlatform,$rootScope) {...} Block bewegen .

$ rootScope ist in Ihrem onNotification-Handler nicht definiert, wo Sie ihn derzeit platzieren. Sie müssen Ihren onNotification-Handler im run() -Block deklarieren, damit er während der Definition auf das rootScope-Objekt zugreifen kann.

Da Sie das rootScope mit einem Event-Handler aktualisieren, der außerhalb des angularen Lifecycle lebt (angle weiß nichts darüber), müssen Sie einen neuen Digest-Zyklus manuell auslösen. In Ihren Benachrichtigungs-Handler, müssen Sie Ihre $rootScope.devicetoken = device_token; Linie mit einem $ rootScope.apply() wickeln, so dass es wie folgt aussieht:

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

kein Glück. Ich habe versucht, onNotification innerhalb zu behalten, aber es wirft unreferenced Fehler – kpvsrkp

+0

Wenn Sie es in den Laufblock verschieben, dann haben Sie Zugang zu einem gültigen $ rootScope-Objekt. Wenn Sie das getan haben, ist etwas anderes undefiniert. Hast du versucht es zu debuggen? – matmo

0

Sie verwenden $ rootScope in window.onNotification so ist es nicht Winkel Zusammenhang so für Sie müssen zu eckig sagen, um updation zu tun. Sie müssen also $scope.$apply(); nach $rootScope updation hinzufügen.

+0

Das ist ... genau das, was ich gesagt habe ... – matmo

+0

Kannst du irgendeinen Link angeben, um die Quelle zu sehen und auszuführen? –

+0

habe ich sowohl $ scope. $ Apply() und $ rootScope. $ Apply() ausprobiert. aber kein Glück. immer noch undefiniert, während er im Controller alarmiert – kpvsrkp

Verwandte Themen