数据绑定单击甚至将当前对象传递给参数

发布于 2025-01-30 01:31:15 字数 785 浏览 2 评论 0原文

我正在通过对象循环,单击时只想发送当前对象。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2025-02-06 01:31:15

您在绑定中调用editcontact。这意味着当ko.applybindings运行时,敲除将其称为一次(通过$ $ root)。 Assuming editContact does not return a function, 单击绑定将被忽略

如果您通过$ root.editcontact而无需调用,则敲除将其调用,然后单击两个参数:当前对象($ data)和单击事件。

data-bind="click: $root.editContact"

如果您的editContact需要请参考$ root,则可以使用.bind

data-bind="click: $root.editContact.bind($root)"

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)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文