我的插件中的Successcallback和Errorcallback

时间:2017-06-02 08:59:08

标签: javascript angularjs ionic-framework

您好我正在尝试在我的插件中调用Successcallback和我的Errorcallback。我使用离子v1似乎它显示了成功和错误的所有控制台..

这是我使用的插件

var exec = require('cordova/exec');
var cordova = require('cordova');

var triangle = {

    initialize: function(applicationId, accessKey, secretKey, successCallback, errorCallback) {
        // Define document events used by the API
        cordova.addDocumentEventHandler('ontaperror');
        cordova.addDocumentEventHandler('ontapsuccess');
        cordova.addDocumentEventHandler('ontapdetect');

        // Call the Android side to initialize the Triangle session
        exec(successCallback, errorCallback, "Triangle", "initialize", [applicationId, accessKey, secretKey]);
    }
}

module.exports = triangle;

这是在我的控制器中调用成功和错误回调

document.addEventListener('deviceready', scanCreditCard, false);

    function scanCreditCard() {

        navigator.triangle.initialize(
            "My Application ID", // application ID
            "Access Key", // access key
            "SecretKey", // secret key
           succcessCallack(),    
           errorCallback() 

        );
    };


    function succcessCallack() {

        console.log("Ready to scan")

        // Subscribe to events that the Triangle APIs raise
        document.addEventListener('ontaperror', onTapError(), false);
        document.addEventListener('ontapdetect', onTapDetect(), false);
        document.addEventListener('ontapsuccess', onNewCard(), false);


    }

    function errorCallback(message) {

        console.log("there was an error initializing the Triangle APIs");
        console.error(message);
        alert("Error")

    }

    function onNewCard(card) {

        console.log("Scanned card successfully.");

        // Display basic card information to the user
        // various other properties such as cardholderName,
        // activationDate, expiryDate, cardPreferredName, and encryptedAccountNumber
        // may be available.
        var dataToShow = card.cardBrand;
        if (card.cardholderName != undefined) {
            dataToShow += "\n" + card.cardholderName;
        }
        dataToShow += "\n" + card.lastFourDigits;
        alert(dataToShow);

    }

    function onTapDetect() {
        console.log("Detected new tap.");

    }


    function onTapError(error) {

        console.log("Error processing contactless card.");
        console.error(error);
    }

1 个答案:

答案 0 :(得分:1)

navigator.triangle.initialize(
    "My Application ID",
    "Access Key",
    "SecretKey",
    succcessCallack,    // remove ()
    errorCallback       // remove ()
);

当您按名称引用功能时,您不需要包括括号。括号表示执行函数并使用其返回值。