-1

I have a global view model which is applied to main div

and I have some other view models and I want to apply them to nested elements of my main div

but I am getting the

You cannot apply bindings multiple times to the same element.

and here it is a sample :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>

    <div id="main">

        <input data-bind="value:title,valueUpdate:'afterkeyup'" />

        <h1 data-bind="text:title"></h1>


        <hr />

        <div id="sub">

            <input data-bind="value:name,valueUpdate:'afterkeyup'" />
            <label data-bind="text:name"></label>

            <!-- a reference to title in globalViewModel -->
            <h1 data-bind="text:title"></h1>

        </div>

    </div>

    <script src="Scripts/knockout-3.2.0.js"></script>
    <script>

        var globalViewModel = {
            title : ko.observable("global title")
        }

        var subViewModel =  {
            name : ko.observable("Test")
        }

        ko.applyBindings(globalViewModel);
        ko.applyBindings(subViewModel, document.getElementById('sub'));

    </script>
</body>
</html>

Please guide me with your brilliant solutions :)

user3153878
  • 421
  • 1
  • 4
  • 6
  • These two lines could be the problem ko.applyBindings(globalViewModel); ko.applyBindings(subViewModel, document.getElementById('sub')); You need to specify the first applybindings statement similar to the second one. The first binding applies binding to the entire page and you are trying to apply binding to some portion of the page (which already has a binding) – G_S Jan 07 '15 at 09:59
  • possible duplicate of [KnockOutJS - Multiple ViewModels in a single View](http://stackoverflow.com/questions/9293761/knockoutjs-multiple-viewmodels-in-a-single-view) – makeitmorehuman Jan 07 '15 at 09:59
  • [Nesting multiple VMs with nested DIVs](http://stackoverflow.com/questions/14270259/nesting-multiple-vms-with-nested-divs) also has a good, concise answer. – janfoeh Jan 08 '15 at 17:35

1 Answers1

0

Need to apply binding separately for both view modal
or can just create both property in same viewModal

Lalji Kanjareeya
  • 135
  • 2
  • 11