ここに、以下の解決策があります。 http://jsfiddle.net/bdellinger/LkqTU/32342/
あなたのポップオーバーのカスタムバインドを作成するのはどうですか?
ko.bindingHandlers.popover = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
ko.bindingHandlers.value.init(element, valueAccessor, allBindings);
var source = allBindings.get('popoverTitle');
var sourceUnwrapped = ko.unwrap(source);
$(element).popover({
trigger: 'focus',
content: valueAccessor(),
title: sourceUnwrapped
});
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var value = valueAccessor();
ko.bindingHandlers.value.update(element, valueAccessor);
}
};
とあなたの関数KOはそれが渡された計算になります。
this.getInfo = ko.computed(function() {
return this.name() + " " + Math.random()
}, this);
、このようなHTMLでそれを呼び出します。
<button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-bind="popover: getInfo, popoverTitle: category">?</button>
以下はすべての解決策です。上記のフィドルリンクをクリックしてください。
javascript。
ko.bindingHandlers.popover = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
ko.bindingHandlers.value.init(element, valueAccessor, allBindings);
var source = allBindings.get('popoverTitle');
var sourceUnwrapped = ko.unwrap(source);
$(element).popover({
trigger: 'focus',
content: valueAccessor(),
title: sourceUnwrapped
});
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var value = valueAccessor();
ko.bindingHandlers.value.update(element, valueAccessor);
}
};
function teh(category, name) {
var self = this;
this.category = ko.observable(category);
this.name = ko.observable(name);
this.getInfo = ko.computed(function() {
return this.name() + " " + Math.random()
}, this);
}
function model() {
var self = this;
this.tehlist = ko.observableArray('');
}
var mymodel = new model();
$(document).ready(function() {
ko.applyBindings(mymodel);
mymodel.tehlist.push(new teh('car', 'honda'));
mymodel.tehlist.push(new teh('dog', 'pug'));
mymodel.tehlist.push(new teh('language', 'spanish'));
mymodel.tehlist.push(new teh('pc', 'dell'));
});
HTML
<table class="table table-striped">
<thead>
<tr>
<th>Category</th>
<th>Name</th>
<th>Get Info</th>
</tr>
</thead>
<tbody data-bind="foreach: tehlist">
<tr>
<td>
<label data-bind="text:category" </td>
<td>
<label data-bind="text:name" </td>
<td>
<button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-bind="popover: getInfo, popoverTitle: category">?</button>
</td>
</tr>
</tbody>
</table>
<div>
あなたはバインディング 'click'に(StackOverflowの)ドキュメントをチェックアウトしましたか? http://stackoverflow.com/documentation/knockout.js/7101/bindings-form-fields/7835/click#t=20161107133431286868 – user3297291
あなたのコメントを確認した後に私の質問を更新しましたが、私はまだ同じ問題を抱えています。 id: – Dana
あなたは同じIDを複数回使用することはできません。foreachは下のhtmlをテンプレートとして使用するので、各リスト項目に同じ 'id'を与えます。バインディング(DOMを扱うための好ましい方法)。あなたがすばやく厄介な修正をしたいだけなら、 'event.target'を使うことができます(お勧めしません)。 – user3297291