Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Here is my code.
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Name Widget.</title>
    <script src="scripts/jquery-2.1.1.min.js"></script>
    <script src="scripts/knockout-3.2.0.js"></script>
    <script>
        $(document).ready(function() {
            ko.components.register('name-widget', {
                viewModel: function (params) {
                    this.name = params.value(); //Getting the bind property.
                    params.value("Sumanth"); //Changing the parent property. (It changing the parent but not child property).
                },
                template: "<p>Your name is:<span data-bind='text: name'></span></p>"
            });

            function nameViewModel() {
                this.myName = ko.observable("Sudheer");
            }

            ko.applyBindings(new nameViewModel());
        })
    </script>
</head>
<body>
    Type in your name: <input type="text" data-bind="value: myName" />
    <span data-bind="text: myName"></span>
    
    <!--Widget-->
    <name-widget params="value: myName"></name-widget>
</body>
</html>


In the above example,
nameViewModel is parent viewmodel and viewModel in component is child view model. I bound the myName observable to value property of child view model , but when i change parent property , it is not updating the child property.
Posted

1 solution

Removing parenthesis in this.name=prams.value worked.

Explanation:

1.By the first way this.name=prams.value() , i'm assigning the value not the dependency.

2. So to assign the observable itself to the child property solved the issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900