可以'我无法使用knockoutJS进行数据绑定

Can't get my data-binding with knockoutJS to work

本文关键字:knockoutJS 数据绑定 可以      更新时间:2023-09-26

我目前正在使用DurandalJS、BreezeJS和KnockoutJS开发一个应用程序。

一切都很顺利,但最简单的事情(我相信(,我无法让它发挥作用。

在我的Viewmodel中,我有一个currentCustomer,它是ko.observable。通过微风我得到了客户!currentCustomer(data.results[0].Customer());一切正常。如果我用谷歌浏览器查看,我会发现对象中充满了currentCustomer。

我想要的是:我有一个inputfield,通过值数据绑定,我试图将currentCustomer的Name绑定到这个输入。但我不能让它发挥作用。什么是有效的:

 <input data-bind="value: currentCustomer()" />

但在输入字段中,它只显示[Object-Object],所以currentCustomer中肯定有一些东西。

这是我尝试过但没有成功的:

<input data-bind="value: currentCustomer().Name()" />
<input data-bind="value: currentCustomer().Name" />
<input data-bind="value: currentCustomer.Name()" />
<input data-bind="value: currentCustomer.Name" />
<input data-bind="value: currentCustomer()._latestValue().Name()" />
<input data-bind="value: currentCustomer()._latestValue.Name()" />

这是一张截图,这样你就可以看到视图中的值了!

http://s22.postimg.org/62m21nnsx/problem_data_bind.png

您尝试过使用'with'吗?

<div data-bind='with:currentCustomer'>
<input data-bind="value: Name/Name()" />
</div>

将其更改为currentCustomer((.name,如果name是currentCustomer实体上的属性,则这应该有效。