我正在努力用淘汰赛来调用一个函数!
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 - 什么都不做。
有人能告诉我我犯的是什么愚蠢的错误吗?
由于
答案 0 :(得分:2)
如果您的代码运行良好,请检查 here 。
一些更正/改进:
data-bind="click: addBank"
无需与addBank()
内部点击功能绑定,这会使click
触发 onLoad 。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