七度&光 2011-09-08 03:25 采纳率: 22.2%
浏览 218
已采纳

可以调用 ko.applyBindings 绑定部分视图吗?

I'm using KnockoutJS and have a main view and view model. I want a dialog (the jQuery UI one) to popup with another view which a separate child view model to be bound to.

The HTML for the dialog content is retrieved using AJAX so I want to be able to call ko.applyBindings once the request has completed, and I want to bind the child view model to just the portion of the HTML loaded via ajax inside the dialog div.

Is this actually possible or do I need to load ALL my views and view models when the page initially loads and then call ko.applyBindings once?

转载于:https://stackoverflow.com/questions/7342814/can-you-call-ko-applybindings-to-bind-a-partial-view

  • 写回答

4条回答 默认 最新

  • 狐狸.fox 2011-09-08 03:38
    关注

    ko.applyBindings accepts a second parameter that is a DOM element to use as the root.

    This would let you do something like:

    <div id="one">
      <input data-bind="value: name" />
    </div>
    
    <div id="two">
      <input data-bind="value: name" />
    </div>
    
    <script type="text/javascript">
      var viewModelA = {
         name: ko.observable("Bob")
      };
    
      var viewModelB = {
         name: ko.observable("Ted")
      };
    
      ko.applyBindings(viewModelA, document.getElementById("one"));
      ko.applyBindings(viewModelB, document.getElementById("two"));
    </script>
    

    So, you can use this technique to bind a viewModel to the dynamic content that you load into your dialog. Overall, you just want to be careful not to call applyBindings multiple times on the same elements, as you will get multiple event handlers attached.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog