Knockout.js 将敲除模板foreach与数据相结合
我想在表中显示几个URL,并在每个URL旁边放置一个删除按钮。由于这是在几个地方使用,我决定使用淘汰模板Knockout.js 将敲除模板foreach与数据相结合,knockout.js,foreach,knockout-templating,Knockout.js,Foreach,Knockout Templating,我想在表中显示几个URL,并在每个URL旁边放置一个删除按钮。由于这是在几个地方使用,我决定使用淘汰模板 <script type="text/html" id="new-repo-template"> <tr class="row"> <td class="url-cell" data-bind="text: repo.url"></td> <td class="button-cell">
<script type="text/html" id="new-repo-template">
<tr class="row">
<td class="url-cell"
data-bind="text: repo.url"></td>
<td class="button-cell">
<button data-bind="click: removeUrl">X</button>
</td>
</tr>
</script>
<table>
<tbody data-bind="template: {name: 'new-repo-template',
foreach: myDataCollection, as: 'repo',
data: {removeUrl: myFunctions.removeRepo } }">
</tbody>
</table>
X
问题是我需要将foreach和函数调用结合起来,我想在其中提供数据,函数调用存储在myFunction对象中,myFunction对象不属于myDataCollection的一部分
是否可以将此foreach与集合数据和数据对象组合,其中所有集合对象都是静态和通用属性
当前设置中,foreach位于数据绑定旁边,导致未设置数据且属性removeUrl未知。是否可以使用基本模板,将
foreach
数据移动到data
属性中,并使用无容器foreach
语法调用迭代。由于模板的作用域是数据
,因此您可以使用$parent
访问该函数
<script type="text/html" id="new-repo-template">
<tr></tr>
<!-- ko foreach: collection -->
<tr class="row">
<td class="url-cell" data-bind="text: url"></td>
<td class="button-cell">
<button data-bind="click: $parent.removeUrl">X</button>
</td>
</tr>
<!-- /ko -->
</script>
<table>
<tbody data-bind="template: {name: 'new-repo-template',
data: {collection: myDataCollection,
removeUrl: myFunctions.removeRepo }
}">
</tbody>
</table>
X
注意:如果没有空的
,foreach
中的数据没有正确标记。不太清楚这是怎么回事
谢谢这太明显了,我找不到。这一排空桌子看起来很奇怪。