数据绑定单击甚至将当前对象传递给参数
我正在通过对象循环,单击时只想发送当前对象。
<div class="box" data-bind="foreach: contacts">
<div class="contact LightGray-background">
<div class="contactType">
<label data-bind="text: contactLabel"></label>
<label style="font-size:smaller" data-bind="text:contactRole"></label>
<img class="hasInfoPopup" data-bind="click: $root.editContact($root)"
data-info="Edit Contact" src="~/images/plus-inverted-large.png" style="height:16px;width:16px;float:right" />
</div>
上面的EditContact()是接收所有对象值,而不是仅一个对象值。
I am looping through objects and I want to send only current object when clicked.
<div class="box" data-bind="foreach: contacts">
<div class="contact LightGray-background">
<div class="contactType">
<label data-bind="text: contactLabel"></label>
<label style="font-size:smaller" data-bind="text:contactRole"></label>
<img class="hasInfoPopup" data-bind="click: $root.editContact($root)"
data-info="Edit Contact" src="~/images/plus-inverted-large.png" style="height:16px;width:16px;float:right" />
</div>
editContact() above is receiving all object values instead of only one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在绑定中调用
editcontact
。这意味着当ko.applybindings
运行时,敲除将其称为一次(通过$ $ root
)。 AssumingeditContact
does not return a function,单击
绑定将被忽略。如果您通过
$ root.editcontact
而无需调用,则敲除将其调用,然后单击两个参数:当前对象($ data
)和单击事件。如果您的
editContact
需要此
请参考$ root
,则可以使用.bind
:You are calling
editContact
in your binding. That means knockout will call it once (passing$root
) whenko.applyBindings
runs. AssumingeditContact
does not return a function, theclick
binding will then be ignored.If you pass
$root.editContact
without calling it, knockout will call it on click with two parameters: the current object ($data
), and the click event.If your
editContact
requiresthis
to refer to$root
, you can use.bind
: