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.
Versuchen Sie, $ rootScope.devicetoken = '' zu definieren; – mtamma