As I understand, $index is available inside a foreach: binding, giving the index of the object... I have a click: binding e.g. click:foo , I need to to access $index inside foo.
Is there a way?
As I understand, $index is available inside a foreach: binding, giving the index of the object... I have a click: binding e.g. click:foo , I need to to access $index inside foo.
Is there a way?
バインディング内の関数を介してハッキングするのではなく、バインディング コンテキストを取得するだけで済みます。バインディングに関連付けられた DOM 要素にアクセスできる限り、ko.contextFor()関数を使用してバインディング コンテキストとそのすべてのプロパティを取得できます。
ハンドラーで取得するイベント オブジェクトにより、プロパティを介してノードにアクセスできtargetます。それを使用してコンテキストを取得します。
var viewModel = {
foo: function (data, event) {
var context = ko.contextFor(event.target);
// do stuff with context.$index()
}
};
はい、
<input type="button" data-bind="click: function(data, event) { myClickHandler($index, data, event); }"/>