我目前正在尝试使用自定义参数调用函数,但是我无法访问该参数,而是返回observable()
对象。基本上我要做的是检索列表中该特定元素的索引。有人能指出我如何做到正确的方向吗?
HTML:
<div id="formula">
<b>Formula</b><br/>
<!-- ko foreach: formula -->
<span data-bind="text: $data, attr: {name: $index}, click: $parent.convert.bind($data,$index)"></span><br/>
<!-- /ko -->
</div>
使用Javascript:
var ListModel = function(formula) {
var self = this;
self.formula = ko.observableArray(formula);
self.convert = function(index) {
alert(index); //this should show the index of the clicked element
}
};
listModel = new ListModel(formula);
ko.applyBindings(listModel);
答案 0 :(得分:8)
应该是$ parent.convert.bind($ data,$ index())
<div id="formula">
<b>Formula</b><br/>
<!-- ko foreach: formula -->
<span data-bind="text: $data, attr: {name: $index}, click: $parent.convert.bind($data,$index())"></span><br/>
<!-- /ko -->
</div>