繁体   English   中英

如何从另一个控制器调用函数?

[英]How to call a function from another Controller?

我正在制作一个应用程序来计算一些预算,所以我有3个控制器。 第二个控制器称为UIConntroller,其工作是从用户那里获取输入并返回到最后一个称为“控制器”的控制器。 然后,从最后一个控制器中,我调用UIConntroller中的get inputInput函数。 当我尝试调用此函数时,出现错误,即UICtrl.getInput不是函数。

我的目标是调用UIConntroller中的函数。



var budgetController = (function () {

} )();


//===========BDGET CONTROLER========== this gets input from the user
var UIConntroller = (function () {
     return {
         getInput: function () {
             return {
             type : document.querySelector('.add__type').value, //will be eithe inc or exp
            description : document.querySelector('.add__description').value,
             value : document.querySelector('.add__value').value
             };
         }
     };
});


//=========GLOBAL APP CONTROLLER========================================
var controller =(function (budgetCtrl,UICtrl) {

    var ctrlAddItem = function () {
            // 1.Get te input field data
            var input = UICtrl.getInput();
            console.log(input);


            //2. Add the item to the budget controller


            //3. Add The Items to the UI


            //4. Calculate the budget 


            //5. Display the budget to the UI


    }

    document.querySelector('.add__btn').addEventListener('click', ctrlAddItem);
//key press event listiner
    document.addEventListener('keypress',function(event){
        if (event.keyCode === 13 || event.which ===13) {
            ctrlAddItem();
        } else {

        }

    })

})(budgetController,UIConntroller);

当我运行代码时,我期望下面的getInput函数被调用,以便它可以接受用户的输入:

getInput: function () {
             return {
             type : document.querySelector('.add__type').value, //will be eithe inc or exp
            description : document.querySelector('.add__description').value,
             value : document.querySelector('.add__value').value
             };
         }

我该如何解决这个问题?

您的IIUI for UIConntroller缺少()来调用它

//===========BDGET CONTROLER========== this gets input from the user
var UIConntroller = (function () {
     return {
         getInput: function () {
             return {
             type : document.querySelector('.add__type').value, //will be eithe inc or exp
            description : document.querySelector('.add__description').value,
             value : document.querySelector('.add__value').value
             };
         }
     };
}());
//^^^ Missing ()

它应该是:

UICtrl().getInput();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM