AngularJS 서비스에서 사용할 일부 자바 스크립트 값에 액세스하려고합니다. 변수에 값을 성공적으로 저장했지만 AngularJS 서비스에 해당 변수를 가져 오는 데 문제가 있습니다.AngularJS의 JS 전역 변수가 정의되지 않음을 반환합니다.
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;
변수 REGID 내가 액세스를 시도하고있는 무슨이다 :
JS 기능 : 여기 내 코드입니다.
AngularJS와 서비스 :
App.service('regID', function()
{
return{}
});
각도 기능 :
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);
}
});
나는이 $의 window.regid를 실행할 때마다 다시 정의되지 않은 온다. 왜 어떤 아이디어?
"전역 변수"라는 용어를 검토하십시오. – user2864740
[JavaScript 함수에서 전역 변수 정의] 가능한 복제본 (http://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function) – user2864740
XY 문제 일 수 있지만 변수, 특히 비동기 연산의 경우 * ick *), 해당 질문에는 "대답"이 포함됩니다. – user2864740