I am looping through objects and I want to send only current object when clicked.
<div data-bind="foreach: contacts">
<div >
<div >
<label data-bind="text: contactLabel"></label>
<label style="font-size:smaller" data-bind="text:contactRole"></label>
<img 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.
CodePudding user response:
You are calling editContact
in your binding. That means knockout will call it once (passing $root
) when ko.applyBindings
runs. Assuming editContact
does not return a function, the click
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.
data-bind="click: $root.editContact"
If your editContact
requires this
to refer to $root
, you can use .bind
:
data-bind="click: $root.editContact.bind($root)"