【问题标题】:Pass two arguments with navigator.notification.confirm?使用 navigator.notification.confirm 传递两个参数?
【发布时间】:2012-12-04 23:49:11
【问题描述】:

我正在尝试使用 Phonegap 通知在我的 Phonegap 应用程序中显示错误消息,然后允许用户通过电子邮件发送错误消息。唯一的问题是我无法将错误消息传递给回调函数,导致电子邮件无用。

我现在的代码如下所示:

function displayError(errormsg) {
    navigator.notification.confirm(
                                   errormsg,
                                   onConfirm,
                                   'Error',
                                   'Submit, Cancel'
                                   );
}
function onConfirm(buttonIndex){
    if (buttonIndex === 1){
        alert(errormsg);
    }

}

displayError("Test")调用,会产生错误内容Test。然后我想将errormsg 传递给onConfirm,但我不知道该怎么做,或者是否可能。

我正在考虑的一个可能的解决方案是:

function displayError(errormsg) {
    test = errormsg
    navigator.notification.confirm(
                                   errormsg,
                                   onConfirm,
                                   'Error',
                                   'Submit, Cancel'
                                   );
}
function onConfirm(buttonIndex){
    if (buttonIndex === 1){
        alert(test);
    }

}

但如果显示新错误,这不会更改 errormsg。我确认了这一点,因为在模拟器设置中,我的应用程序抛出了两个错误。第一个在使用该方法时可以正常工作,传递test,但紧随其后的第二个错误使用原始变量,而不是最近的变量。

【问题讨论】:

    标签: javascript cordova cordova-2.0.0


    【解决方案1】:
    function displayError(errormsg) {
        navigator.notification.confirm(
            errormsg,
            function(buttonIndex){
                onConfirm(buttonIndex, errormsg);
            },
            'Error',
            'Submit, Cancel'
            );
    }
    function onConfirm(buttonIndex, errormsg){
        if (buttonIndex === 1){
            alert(errormsg);
        }
    
    }
    

    将它包装在一个匿名函数中怎么样?这样,您可以传递任意数量的参数,同时保持范围。

    【讨论】:

    • 这在您需要保持范围并使用var self = this 而不是this 时也很有帮助。谢谢你的主意!
    • 我想知道为什么 22 位用户赞成这个回复,但没有赞成这个问题。这个网站的评级系统还有很多不足之处......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 2014-04-10
    • 2012-08-26
    相关资源
    最近更新 更多