4

我目前正在尝试使用自定义参数调用函数,但是我无法访问该参数,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);
4

1 回答 1

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>

例子

于 2012-06-11T08:28:47.030 回答