【问题标题】:AngularJS Calling a service not workingAngularJS调用服务不起作用
【发布时间】:2023-03-04 04:54:01
【问题描述】:

我正在尝试从函数“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{};
});

我应该如何在这个函数中调用服务?我的 Angular 知识有限。任何帮助将不胜感激。

更新:响应用户 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);
}
}); 

【问题讨论】:

  • 什么是onNotification?它是控制器还是什么?你是如何在那里注入一些东西的,例如你如何通过$scope
  • onNotification 是一个位于任何控制器之外的函数(如果我将它放在控制器中它就不起作用)。并且我通过将服务名称和 $scope 放在函数名称“onNotification(e,$scope,currentUser)”之后的括号中来通过正常方式(在其他地方工作)注入它
  • 你能显示你打电话给onNotification的地方吗? e 是什么?那是服务名称吗?您必须传入 $scope 权限
  • 我不知道这个插件是什么.. 但是尝试发送currentUser 作为键值对,就像发送badge sound 等一样。
  • PushPlugin 中的插件,用于向苹果和安卓设备发送推送通知。我已经尝试了您建议的方法,但无济于事(仍然未定义)。以某种方式提取 e.regid (我需要的另一个值)并尝试将其推送到一个数组中以及任何想法我将如何去做是一个想法吗?

标签: javascript angularjs


【解决方案1】:

尝试改变

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) {

   }

}]);

【讨论】:

  • 不,它仍然返回未定义。它与它是一项服务这一事实有关吗?
  • onNotification 在哪里定义?它在控制器内部吗?
  • 不,我试图将它放在同一个控制器中,但它告诉我未定义 onNotification。
  • 抱歉回复晚了,需要睡觉。我已经按照你说的做了,它会抛出一个错误,如果我使用 $scope.onNotification,它会返回“$scope is not defined”,如果我使用函数 onNotification,我会得到 onNotification is not defined。
猜你喜欢
  • 1970-01-01
  • 2016-01-09
  • 2015-10-29
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多