0

I'm trying to access a value inside some Javascript to use in an AngularJS service. I have stored the value inside a variable successfully but I am having issues getting that variable into my AngularJS service. Here is my code:

JS function:

 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;

The variable regid is what I am trying to access.

AngularJS Service:

App.service('regID', function()
{
return{}
});

Angular Function:

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);
}   
});

Every time I run this $window.regid comes back as undefined. Any ideas as to why?

geolaw
  • 412
  • 1
  • 12
  • 26
  • 1
    Please review the term "global variable". – user2864740 Sep 04 '14 at 22:22
  • possible duplicate of [Define global variable in a JavaScript function](http://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function) – user2864740 Sep 04 '14 at 22:23
  • While it might be an XY issue (global variables, especially with async operations, are *ick*), that question contains the "answer". – user2864740 Sep 04 '14 at 22:24

1 Answers1

0

So you have not assigned it to a global variable. You have assigned it to a local function scope variable.

To assign it to a global variable use window.regID rather than var regID.

Your regID service does nothing. It should return $window.regID.

This all being said, this is not the right approach. The correct approach would be a service that returns a promise. The service also listen to native javascript or jqlite events, and resolve the promise when the event is handled.

Martin
  • 15,820
  • 4
  • 47
  • 56
  • do you have any exmaples of how to make my service do as you said, my knowledge of angular is extremely limited. Thank you for the response – geolaw Sep 04 '14 at 23:08
  • this has done the trick. Thanks had me stumped for a while. Much appreciated. – geolaw Sep 04 '14 at 23:20
  • @geolaw let me know what event is triggering the onNotification() handler and I'll update the service. Another thing you will want to get rid of is the $().append() DOM manipulation in your controllers. A scope variable that is displayed in the view or ngMessage for 1.3 should be used. – Martin Sep 05 '14 at 07:46
  • the issue has been resolved, onNotification is triggered at "'ecb':'onNotification'" I have removed the service as it does not need to be updated I have managed to update the global variable inside another state controller. I will look into removing the "$().append()" parts of the code, I was not aware that was DOM manipulation. – geolaw Sep 05 '14 at 15:23