AngularJS调用服务无法正常工作

时间:2014-09-02 22:50:45

标签: javascript angularjs

我试图从函数“onNotification(e,$ scope,currentUser)”中调用一个服务但是每次我尝试并记录它时,它都会返回错误:

  

processMessage失败:错误:TypeError:无法设置未定义的属性“当前”

或者如果我使用“var userRegistration = this.currentUser”我在日志中得到“未定义”,但是我知道该服务正在更新,因为当我将结果记录在函数之外时它返回正确的值。

这是我的代码:

function onNotification(e, $scope, currentUser) {
var Currentuser = this.currentUser;

$("#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.

        console.log(Currentuser);

        console.log("regID = " + e.regid);
    }
break;

case 'message':
    // if this flag is set, this notification happened while we were in the foreground.
    // you might want to play a sound to get the user's attention, throw up a dialog, etc.
    if ( e.foreground )
    {
        $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');

        // on Android soundname is outside the payload. 
        // On Amazon FireOS all custom attributes are contained within payload
        var soundfile = e.soundname || e.payload.sound;
        // if the notification contains a soundname, play it.
        var my_media = new Media("/android_asset/www/"+ soundfile);
        my_media.play();
    }
    else
    {  // otherwise we were launched because the user touched a notification in the notification tray.
        if ( e.coldstart )
        {
            $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
        }
        else
        {
            $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
        }
    }

   $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');

   $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
break;

case 'error':
    $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
break;

default:
    $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
break;
}
}

这是同一个js文件中的服务:

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

我该如何调用此函数内的服务?我的角色知识有限。任何帮助将不胜感激。

更新:响应用户PSL。在这里调用onNotification:

App.controller('LogHomeCtrl', function($scope, $log, $state, currentUser)
{
var pushNotification;

document.addEventListener("deviceready", onDeviceReady, false);

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)
{
    alert('result = ' + result);
}

function errorHandler (error)
{
    alert('error = ' + error);
}
}); 

1 个答案:

答案 0 :(得分:0)

尝试更改

var Currentuser = this.currentUser;

var Currentuser = currentUser;

currentUser不属于onNotification它仅作为参数传入,因此被定义为其本地范围内的变量。但不是自己的财产

修改

onNotification应该在控制器内定义。例如

yourModuleName.controller('controllerName', ['$scope', 'currentUser', function($scope, currentUser) {

   $scope.currentUser = currentUser;

   $scope.onNotification = function(e, $scope, $scope.currentUser) {

   }

}]);