knockout cant进程点击绑定不是一个函数

时间:2015-09-21 09:37:18

标签: knockout.js

我正在努力用淘汰赛来调用一个函数!

var bankViewModel = function () {
    self.addBank = function(){
                return function(){
                    self.addShow(true);
                    var bank = new observableBank('',"","","","","","","","","Active");
                    self.newBank(bank);
                };
        };
};

var bankViewModelInstance = new bankViewModel();
ko.applyBindings(bankViewModelInstance, document.getElementById("company-info-bank"));

在视图中我尝试了大量的绑定变体: -

<button id="demo-btn-addrow" class="btn btn-purple btn-labeled fa fa-plus" data-bind="click: addBank()">Add New</button>

尝试$ parent(undefined),$ data - 什么都不做。

有人能告诉我我犯的是什么愚蠢的错误吗?

由于

1 个答案:

答案 0 :(得分:2)

如果您的代码运行良好,请检查 here

一些更正/改进:

  • 您可以使用data-bind="click: addBank"无需与addBank()内部点击功能绑定,这会使click触发 onLoad
  • 在viewModel中单击功能逻辑,您无需返回function()

<强>视图模型:

var bankViewModel = function () {
    var self = this;
    self.newBank = ko.observableArray();
    self.addShow = ko.observable();
    self.addBank = function () {
        self.addShow(true);
        /*                    var bank = new observableBank('',"","","","","","","","","Active"); */
        self.newBank.push(1); //for testing

    };
};

var bankViewModelInstance = new bankViewModel();
ko.applyBindings(bankViewModelInstance);

示范工作小提琴 here