KO.js 模型绑定。从孙子调用视图模型中的函数不起作用
KO.js model binding. Calling a function in the viewmodel from a grand child is not working
我有以下模型绑定。
<!-- ko foreach: RctApplyVacancy.RctVacancies -->
<tr>
// Some TD elements
// Following binding works
<td class="rct-vmiddle text-center" style="width: 8%;">
<input type="button" data-bind="click: RctApplyVacancy.Apply" />
</td>
<td class="rct-vmiddle text-center" style="width: 4%;">
<a data-toggle="popover" data-container="body" data-placement="right"
type="button" data-html="true" href="#" data-bind="attr: { id: 'Share' + AdvId() }">
<i class="fa fa-share-alt" aria-hidden="true"></i>
</a>
<div data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide">
<ul class="list-group">
<li data-bind="click : RctApplyVacancy.Apply">
<i class="classes" aria-hidden="true"></i></li> // This won't
</ul>
</div>
</td>
</tr>
<!-- /ko -->
根据评论,绑定在后半部分(请按照评论This won't
)不起作用。有人可以解释一下为什么吗?我在这里做错了什么?
P.S 如果需要,请在评论中索取更多详细信息。为了清楚起见,我省略了 ViewModel。
foreach
makes the item for the iteration the current item and resolves relative to it. See the documentation,如果要引用父项(或其父项),则使用 $parent
(或 $parents[1]
用于祖父项)。看看你的标记,我会说你想要 $parent.RctApplyVacancy.Apply
.
原来是 Bootstrap's popover plugin 的问题。正如在问题中看到的那样,<li data-bind="click : RctApplyVacancy.Apply">
包含在 <div>
中,渲染后看起来像 <div data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide" id="popover-content6"> ... </div>
。此 ID 位于应用程序的不同位置,用于使用 Bootstrap 的弹出窗口插件生成弹出窗口。
我试过 运行 这个没有 data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide"
部分(即 <div>
标签的简单外观)。那工作完美无缺。我尝试了上述内容,但没有用。看起来,popover 剥离了模型绑定,使子项与函数的绑定不再起作用。但是 这似乎对子项中的数据绑定没有影响。每个数据绑定似乎都有效。
EDIT1
上面最后一句好像是无效的。因为即使出现了绑定,它的值似乎也不正确。等待进一步的事实。
我有以下模型绑定。
<!-- ko foreach: RctApplyVacancy.RctVacancies -->
<tr>
// Some TD elements
// Following binding works
<td class="rct-vmiddle text-center" style="width: 8%;">
<input type="button" data-bind="click: RctApplyVacancy.Apply" />
</td>
<td class="rct-vmiddle text-center" style="width: 4%;">
<a data-toggle="popover" data-container="body" data-placement="right"
type="button" data-html="true" href="#" data-bind="attr: { id: 'Share' + AdvId() }">
<i class="fa fa-share-alt" aria-hidden="true"></i>
</a>
<div data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide">
<ul class="list-group">
<li data-bind="click : RctApplyVacancy.Apply">
<i class="classes" aria-hidden="true"></i></li> // This won't
</ul>
</div>
</td>
</tr>
<!-- /ko -->
根据评论,绑定在后半部分(请按照评论This won't
)不起作用。有人可以解释一下为什么吗?我在这里做错了什么?
P.S 如果需要,请在评论中索取更多详细信息。为了清楚起见,我省略了 ViewModel。
foreach
makes the item for the iteration the current item and resolves relative to it. See the documentation,如果要引用父项(或其父项),则使用 $parent
(或 $parents[1]
用于祖父项)。看看你的标记,我会说你想要 $parent.RctApplyVacancy.Apply
.
原来是 Bootstrap's popover plugin 的问题。正如在问题中看到的那样,<li data-bind="click : RctApplyVacancy.Apply">
包含在 <div>
中,渲染后看起来像 <div data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide" id="popover-content6"> ... </div>
。此 ID 位于应用程序的不同位置,用于使用 Bootstrap 的弹出窗口插件生成弹出窗口。
我试过 运行 这个没有 data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide"
部分(即 <div>
标签的简单外观)。那工作完美无缺。我尝试了上述内容,但没有用。看起来,popover 剥离了模型绑定,使子项与函数的绑定不再起作用。但是 这似乎对子项中的数据绑定没有影响。每个数据绑定似乎都有效。
EDIT1 上面最后一句好像是无效的。因为即使出现了绑定,它的值似乎也不正确。等待进一步的事实。