How to call ko.applyBindings to bind a partial view ?

How to call ko.applyBindings to bind a partial view ?

Asked on November 14, 2018 in knockout js.
Add Comment


  • 3 Answer(s)

      This ko.applyBindings tolerate a second parameter that is a DOM element to use as the root.\

    Try this code:

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

       This technique help to bind a viewModel to the dynamic content.But do not call applyBindings multiple times on the same elements, otherwise multiple event handlers attached to the result.

    Answered on November 14, 2018.
    Add Comment

      Use this code:

    <div>
      <input data-bind="value: VMA.name" />
    </div>
    <div>
      <input data-bind="value: VMB.name" />
    </div>
    <script type="text/javascript">
      var viewModels =
    {
        VMA: {name: ko.observable("Bob")},
        VMB: {name: ko.observable("Ted")}
      };
      ko.applyBindings(viewModels);
    </script>
    

      From this: specify the DOM element is not needed and  bind multiple models to the same element is possible, like this:

    <div>
      <input data-bind="value: VMA.name() + ' and ' + VMB.name()" />
    </div>
    
    Answered on November 14, 2018.
    Add Comment

    Get the needed details with the with binding, as well as controlsDescendantBindings.

    Answered on November 14, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.