我想使用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');
}
但我在控制器中發出警報時未定義。
嘗試定義$ rootScope.devicetoken = ''; – mtamma