可观察对象不显示某些属性 [英] Observable object not displaying some properties

查看:93
本文介绍了可观察对象不显示某些属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的视图模型,

I have a viewmodel like this,

        function viewModel() {
            this.Items = ko.observable({ A : 1, B: 2 });
            this.Items.X = 4;
            this.Items.Y = 5;
        }

        ko.applyBindings(new viewModel());

我正在打印出这样的值,

I am printing out the values like this,

        <span data-bind="text: $root.Items['A']"></span>
        <span data-bind="text: $root.Items['B']"></span>
        <span data-bind="text: $root.Items['X']"></span>
        <span data-bind="text: $root.Items['Y']"></span>

显示X和Y值,但A和B值为空吗?

X and Y values are displayed, but A and B values are empty?

推荐答案

您正在将X和Y设置为可观察函数的一部分,而不是被观察的对象.

You're setting X and Y as part of the observable function, instead of the object being observed.

function viewModel() {
   this.Items = ko.observable({ A : 1, B: 2 });
   this.Items().X = 4;
   this.Items().Y = 5;
}

ko.applyBindings(new viewModel());

this.Items()将提供对对象的访问.

this.Items() will provide access to the object.

<span data-bind="text: $root.Items()['A']"></span>
<span data-bind="text: $root.Items()['B']"></span>
<span data-bind="text: $root.Items()['X']"></span>
<span data-bind="text: $root.Items()['Y']"></span>

这篇关于可观察对象不显示某些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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