gpt4 book ai didi

javascript - Knockout JS 如何知道要传递的正确参数?

转载 作者:行者123 更新时间:2023-11-28 20:01:44 24 4
gpt4 key购买 nike

我对以下内容有很大疑问:假设我们分别有以下 View 模型和 View

function TableViewModel() {
var self = this;

self.someObservableCollection = ko.observableArray();

self.doWorkWithObjectInCollection(collectionObject) {
/* Code to process the collectionObject */
}
}

ko.applyBindings(new TableViewModel());

View 就像这样(假设我们还运行了一个函数来获取集合的初始数据):

<table>
<thead> <!-- Table head here --> </thead>
<tbody data-bind="foreach: someObservableCollection">
<tr>
<td data-bind="text: property1"></td>
<td data-bind="text: property2"></td>
<!-- ... -->
<td><a data-bind="click: $parent.doWorkWithObjectInCollection">Do Work</a></td>
</tr>
</tbody>
</table>

现在,非常有趣的是,当单击“Do Work”链接时,会调用函数doWorkWithObjectInCollection并且传递的参数是当前行中使用的对象.

Knockout如何知道我们要传递的参数是foreach循环中的当前对象?在其他情况下,我如何知道什么 knockout 将作为参数传递给在某些事件(例如单击)上调用的函数?

最佳答案

Knockout 不知道您想要传递集合中的对象,它在文档中告诉您它将执行此操作。您无法直接影响此行为。

请参阅 http://knockoutjs.com/documentation/click-binding.html 处的“注释 1”

关于javascript - Knockout JS 如何知道要传递的正确参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21554741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com