2016-05-16 2 views
0

rootscopeをグローバルとして使用しようとしているので、コントローラで同じものを取り出すことができます。window.onnotification内でrootScopeを使用できません

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

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

がwindow.onNotificationあなたはあなたの.run(function($ionicPlatform,$rootScope) {...}ブロックの内側にあなたのwindow.onNotification = function(e){..}宣言を移動する必要があります$ scope.proceed

+0

$ rootScopeは、角度の付いたグローバルなオブジェクトです。それを自分で初期化する必要はありません。そうしても、オブジェクトになります。 – matmo

+0

ありがとう、コードを更新してください: – mtamma

0

前に実行していることを確認し、実行スコープに$ rootScope.devicetokenを定義しよう。

$ rootScopeは、現在配置されているonNotificationハンドラでは未定義です。定義中にrootScopeオブジェクトにアクセスできるように、run()ブロック内でonNotificationハンドラを宣言する必要があります。

また、角度ライフサイクルの外にある(角が分からない)イベントハンドラでrootScopeを更新するため、新しいダイジェストサイクルを手動で呼び出す必要があります。それは次のようになりますように、あなたの通知ハンドラでは、あなたは、$ rootScope.apply()を使用して$rootScope.devicetoken = device_token;行をラップする必要があります。

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

運がいいえ。私はonNotificationを内部に保存しようとしましたが、参照されていないエラーを投げています – kpvsrkp

+0

runブロックに移動すると、有効な$ rootScopeオブジェクトにアクセスできます。もしあなたがそれをしたら、それ以外のものは未定義です。あなたはそれをデバッグしようとしましたか? – matmo

0

あなたはwindow.onNotificationで$ rootScopeを使用しているので、それは角のコンテキストではないので、あなたは更新をするために角張っていることを伝える必要があるからです。 $rootScopeの更新後に$scope.$apply();を追加する必要があります。

+0

これは...まさに私が言ったことです... – matmo

+0

ソースを見て実行するためのリンクを提供できますか? –

+0

私は$ scope。$ apply()と$ rootScope。$ apply()の両方を試しました。運がない。コントローラ内で警告中に未定義 – kpvsrkp

関連する問題