Knockout.js如何访问数据绑定上的内部对象属性 [英] Knockout.js how to access the inner object property on data-bind

查看:280
本文介绍了Knockout.js如何访问数据绑定上的内部对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个非常简单的基因剔除代码:

I've a super simple knockout code below:

var data ={
    "Id" : 1001,
    "SalePrice" : 12345,
    "ListPrice" : 333,
    "ShortDesc" : "Tayler 12345E",
    "Description" : " Long Description"
};


var viewModel={
    dataTest: ko.observable(data)
};


ko.applyBindings(viewModel);

<span data-bind="text: dataTest.SalePrice"></span>

基本上,我已将整个数据对象传递给可观察的属性dataTest.我以为用点表示法访问内部对象的属性很容易.但是不是.

Basically, I've passed the whole data object to observable property dataTest. I would have thought it's easy enough to access the property of inner object with dot notation. But's not.

有人可以帮忙使上面的代码起作用吗?

Could someone please help how to make above code to work?

推荐答案

现在可以观察到(即,它是一个函数).您需要调用该函数.

It's an observable now (ie, it's a function). You need to call the function.

<span data-bind="text: dataTest().SalePrice"></span>

编辑Akshat的小提琴以进行匹配.

但是,不知道为什么这比嵌套适当的层次结构有优势. OP要求的是非常反模式的.我认为您可能想查看有关绑定上下文Akshat建议的页面,以了解如何创建dataTest作为父对象,然后在标记中向下钻取.

Not sure why this is an advantage over nesting a proper hierarchy, however. What the OP is asking is pretty anti-pattern. I think you might want to look over the page on binding context Akshat suggests to see how to create the dataTest as a parent object and then drill down in your markup.

这篇关于Knockout.js如何访问数据绑定上的内部对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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