我在使用事件将元素加载到我的项目 Observable-Array 时遇到问题。
ViewModel = (function () {
var
items = ko.observableArray([]),
removeItems = function (element) {
items.remove(element);
},
saveAll = function () {
return ko.toJS(items);
},
addItem = function (element) {
items.push(element);
return false; // no Page-Reload after button-klick
};
return {
Items: items,
// i call addItem with a dummy object (for testing)
clickSave: addItem(new Customer(1, "Tfsd", "Tfsd"))
};
})();
( fiddle )
为什么调用 addItem 函数,甚至没有单击按钮?是因为函数末尾的()吗?
addItem = function (element) {
items.push(element);
return false; // no Page-Reload after button-click
};
我该怎么做才能只为事件做这个? 还是我的问题出在其他地方?
最佳答案
使用
return {
Items: items,
clickSave: addItem
};
关于javascript - Knockout.js:将元素添加到可观察数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10882389/