Javascript AngularJS中的JS全局变量返回未定义
我试图访问一些Javascript中的值,以便在AngularJS服务中使用。我已成功地将值存储在变量中,但在将该变量放入AngularJS服务时遇到问题。这是我的密码: JS功能:Javascript AngularJS中的JS全局变量返回未定义,javascript,angularjs,phonegap-pushplugin,Javascript,Angularjs,Phonegap Pushplugin,我试图访问一些Javascript中的值,以便在AngularJS服务中使用。我已成功地将值存储在变量中,但在将该变量放入AngularJS服务时遇到问题。这是我的密码: JS功能: function onNotification(e) { $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>'); switch( e.event ) { case 'registered'
function onNotification(e) {
$("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
$("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
var regid = e.regid
console.log(regid);
}
break;
角函数:
App.controller('MainCtrl', function($scope, $state, regID, $window){
console.log('MainCtrl');
var pushNotification;
document.addEventListener("deviceready", onDeviceReady, false);
$scope.regID = regID;
$scope.regID.regID = $window.regid;
console.log($scope.regID);
function onDeviceReady()
{
pushNotification = window.plugins.pushNotification;
$("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
if ( device.platform == 'android' || device.platform == 'Android'){
pushNotification.register(
successHandler,
errorHandler,
{
"senderID":"460885134680",
"ecb":"onNotification",
});
} else {
pushNotification.register(
tokenHandler,
errorHandler,
{
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPN"
});
}
}
function successHandler (result, $scope, regID, $window)
{
alert('result = ' + result);
}
function errorHandler (error)
{
alert('error = ' + error);
}
});
App.controller('MainCtrl',函数($scope、$state、regID、$window){
console.log('MainCtrl');
通知;
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
$scope.regID=regID;
$scope.regID.regID=$window.regID;
console.log($scope.regID);
函数ondevicerady()
{
pushNotification=window.plugins.pushNotification;
$(“#应用程序状态ul”).append(“注册”+device.platform+” );
if(device.platform==“android”| | device.platform==“android”){
pushNotification.register(
成功者,
错误处理程序,
{
“senderID”:“460885134680”,
“欧洲央行”:“通知”,
});
}否则{
pushNotification.register(
令牌处理程序,
错误处理程序,
{
“徽章”:“真实”,
“声音”:“真实”,
“警报”:“正确”,
“欧洲央行”:“通知”
});
}
}
函数successHandler(结果,$scope,regID,$window)
{
警报(“结果=”+结果);
}
函数errorHandler(错误)
{
警报(“错误=”+错误);
}
});
每次我运行$window.regid时,它都返回为未定义。想知道为什么吗?所以您没有将其分配给全局变量。您已将其分配给局部函数范围变量 要将其分配给全局变量,请使用window.regID而不是var regID 您的注册服务没有任何作用。它应该返回$window.regID
尽管如此,这不是正确的方法。正确的方法是返回承诺的服务。该服务还侦听本机javascript或jqlite事件,并在处理事件时解析承诺 请回顾“全局变量”一词。虽然它可能是一个XY问题(全局变量,尤其是异步操作的全局变量),但该问题包含“答案”。您是否知道如何使我的服务如您所说那样,我对angular的知识非常有限。谢谢你的回答。这已经成功了。多谢了我一段时间。非常感谢。@Geolow让我知道是什么事件触发了onNotification()处理程序,我将更新服务。另一件您想要摆脱的事情是控制器中的$().append()DOM操作。应使用在1.3的视图或ngMessage中显示的范围变量。问题已解决,onNotification在“'ecb':'onNotification'”处触发。我已删除服务,因为它不需要更新。我已设法在另一个状态控制器内更新全局变量。我将研究删除代码中的“$().append()”部分,我不知道这是DOM操作。
App.controller('MainCtrl', function($scope, $state, regID, $window){
console.log('MainCtrl');
var pushNotification;
document.addEventListener("deviceready", onDeviceReady, false);
$scope.regID = regID;
$scope.regID.regID = $window.regid;
console.log($scope.regID);
function onDeviceReady()
{
pushNotification = window.plugins.pushNotification;
$("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
if ( device.platform == 'android' || device.platform == 'Android'){
pushNotification.register(
successHandler,
errorHandler,
{
"senderID":"460885134680",
"ecb":"onNotification",
});
} else {
pushNotification.register(
tokenHandler,
errorHandler,
{
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPN"
});
}
}
function successHandler (result, $scope, regID, $window)
{
alert('result = ' + result);
}
function errorHandler (error)
{
alert('error = ' + error);
}
});