Javascript 无法在window.onnotification内使用rootScope
我正在尝试将rootScope用作全局的,以便可以在控制器中检索到它 app.js:Javascript 无法在window.onnotification内使用rootScope,javascript,angularjs,ionic-framework,Javascript,Angularjs,Ionic Framework,我正在尝试将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 ba
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');
}
我正在获取设备\u令牌并进入警报状态。但在控制器中使用它并不是在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>'});
}
})
在控制器中发出警报时,我仍然没有定义。尝试在运行范围中定义$rootScope.devicetoken
.run(function($ionicPlatform,$rootScope) {
$rootScope.devicetoken = '';
确保window.onNotification在$scope之前执行。继续您需要将
window.onNotification=function(e){..}
声明移动到块内。运行(function($ionicPlatform,$rootScope){..}
块
$rootScope将在您当前放置的onNotification处理程序中未定义-您需要在run()块中声明onNotification处理程序,以便在定义它时它可以访问rootScope对象
此外,由于您将使用angular生命周期之外的事件处理程序更新rootScope(angular不知道),因此需要手动调用trigger以触发新的摘要周期。在通知处理程序中,您需要使用$rootScope.devicetoken=device_token;行包装$rootScope.apply(),所以它看起来像这样:
$rootScope.apply(function(){
$rootScope.devicetoken = device_token;
});
您正在
窗口.onNotification中使用$rootScope,因此它不是angular上下文,因此您需要告诉angular进行更新。
因此,您需要添加$scope.$apply()
在$rootScope
更新之后。尝试定义$rootScope.devicetoken='';$rootScope是一个全局的、角度提供的对象。您不必自己初始化它,即使初始化了,它也会是一个对象。您能提供任何链接来查看和运行源代码吗?我尝试了$scope.$apply()和$rootScope.$apply()。但没有运气。在向内部控制器发出警报时仍然未定义。没有运气。我试图将onNotification保留在内部,但它会抛出未引用的错误。如果将其移动到run块中,则您将有权访问有效的$rootScope对象。如果您这样做,则其他内容未定义。您尝试过调试它吗?
$rootScope.apply(function(){
$rootScope.devicetoken = device_token;
});