knockout.js在MVC -不工作绑定在html表



为什么我不能在一些表的标签中为按钮做点击绑定?如果我把按钮移到表外它工作吗?

<td>
<div>
You've clicked <span data-bind="text: numberOfClicks"></span> times
<button data-bind="click: incrementClickCounter">Click me</button>
</div>

</td>

vm:

define(['viewmodels/shell', 'durandal/services/logger', 'plugins/dialog', 'viewmodels/shell', 'toastr', 'knockout', 'kovalidationconfig', 'plugins/router', 'typeahead.bundle'],
function (shell, logger, dialog, shell, toastr, ko, kvc, router, typeahead) {
var vm = {
activate: activate,
shell: shell,
data: ko.observableArray([]),
close: function () {
$(window).off('popstate', vm.goBack);
$(window).off('resize', adjustModalPosition);
dialog.close(vm, 'cancel');
},
goBack: function () {
$(window).off('popstate', vm.goBack);
$(window).off('resize', adjustModalPosition);
dialog.close(vm, 'back');
},
editPreregisteredChildren: function () {
router.navigate("#/function/" + this.id);
},
incrementClickCounter : function() {
var previousCount = this.numberOfClicks();
this.numberOfClicks(previousCount + 1);
}
currentPage: ko.observable(1),
itemsPerPage: ko.observable(10),
hasNextPage: ko.observable(false),
previousPage: previousPage,
nextPage: nextPage,
searchCriteria: ko.observable(''),
applySearch: applySearch,
locations: ko.observableArray([]),
locationId: ko.observable(),
LocationName: ko.observable(),
exportHref: ko.observable("/spa/ExportSchedulings"),
bindingComplete: function (view) {
bindFindLocationEvent(view);
}
};
...
)};

表体可能在数组上迭代,并且每行表示数组项,而不是根vm。你需要绑定到"click: $parent.incrementClickCounter"或者"点击:$root. incrementclickcounter"。

您是否可能处于嵌套状态?当我绑定到具有多个视图模型的视图时,我有时会遇到这种情况。尝试添加data-bind='with: nameOfTheViewModel'到表数据标签:EG:

<td data-bind='with: nameOfTheViewModel'>
<div>
You've clicked <span data-bind="text: numberOfClicks"></span> times
<button data-bind="click: incrementClickCounter">Click me</button>
</div>

</td>

可能还需要附加$Parent。数据绑定= ':美元Parent.nameOfTheViewModel '