【问题标题】:KO.js model binding. Calling a function in the viewmodel from a grand child is not workingKO.js 模型绑定。从孙子调用视图模型中的函数不起作用
【发布时间】:2018-08-20 05:08:27
【问题描述】:

我有以下模型绑定。

    <!-- 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 -->

根据 cmets,后半部分的绑定(请关注评论 This won't)不起作用。有人可以解释一下为什么吗?我在这里做错了什么?

P.S 如果需要,请在 cmets 中请求更多详细信息。为了清楚起见,我省略了 ViewModel。

【问题讨论】:

  • “为了清楚起见,我省略了 ViewModel。” 简洁,也许;不清晰。 (下一次,包括一个minimal reproducible example。)
  • 糟糕!选词错误。 :D 是的,它很简洁.. 感谢您的提示..

标签: javascript knockout.js viewmodel model-binding


【解决方案1】:

foreach 使迭代的项目成为当前项目并相对于它进行解析。请参阅the documentation,如果您想引用父项(或其父项),请使用$parent(或$parents[1] 用于祖父项)。查看您的标记,我会说您想要$parent.RctApplyVacancy.Apply

【讨论】:

    【解决方案2】:

    原来是Bootstrap's popover plugin 的问题。正如在问题中看到的那样,&lt;li data-bind="click : RctApplyVacancy.Apply"&gt; 包含在一个&lt;div&gt; 中,一旦呈现,它看起来就像&lt;div data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide" id="popover-content6"&gt; ... &lt;/div&gt;。此 ID 在应用程序的不同位置,用于使用 Bootstrap 的弹出框插件生成弹出框。

    我尝试在没有data-bind="attr: { id: 'popover-content' + AdvId() }" class="hide" 部分的情况下运行它(即&lt;div&gt; 标记在其简单的外观中)。那工作完美无缺。我尝试了上述内容,它没有工作。看起来,popover 剥离了模型绑定,使得子函数与函数的绑定不再起作用。但是这似乎对子项中的数据绑定没有影响。每个数据绑定似乎都有效。

    EDIT1 上面的最后一句话似乎无效。因为即使出现了绑定,它的值似乎也不正确。等待进一步的事实。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 2013-06-19
      • 1970-01-01
      • 2016-06-14
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多