父视图模型属性,可观察分配给子视图模型属性属性,在子视图模型中更改时更新父项但不更新子属性。 [英] Parent view model property which is observable assigned to child view model property property, when changed in child view model updating the parent but not updating the child property.

查看:55
本文介绍了父视图模型属性,可观察分配给子视图模型属性属性,在子视图模型中更改时更新父项但不更新子属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我的代码。

Hi,

Here is my code.

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





在上面的例子中,

nameViewModel是父视图模型,组件中的viewModel是子视图模型。我将myName observable绑定到子视图模型的value属性,但是当我更改父属性时,它不会更新子属性。



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.

推荐答案

document )。ready( function (){
ko.components.register(' name-widget',{
viewModel: function ( params){
this .name = params.value(); // 获取绑定属性。
params.value( Sumanth ); // 更改父属性。(更改父属性但不更改子属性)。
},
模板: < p>您的姓名是:< span data-bind ='text:name'>< ; / span>< / p>
});

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

ko.applyBindings( new nameViewModel());
})
< / script >
< / head >
< body >
输入您的姓名:< 输入 type = text data-bind = value:myName / >
< span data-bind = text:myName > < / span > ;

<! - 小工具 - >
< < span class =code-leadattribute> name-widget params = value:myName > < / name-widget >
< / body >
< / html >
(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>





在上面的例子中,

nameViewModel是父视图模型,组件中的viewModel是子视图模型。我将myName observable绑定到子视图模型的value属性,但是当我更改父属性时,它不会更新子属性。



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.


删除this.name = prams.value中的括号。



说明:



1.通过第一种方式this.name = prams.value() ,我分配的值不是依赖。



2.所以将observable本身分配给子属性解决了这个问题。
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.


这篇关于父视图模型属性,可观察分配给子视图模型属性属性,在子视图模型中更改时更新父项但不更新子属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆