剔除数据绑定到数组元素仅工作一半 [英] Knockout Data-Bind to Array Element Only Half-Working

查看:68
本文介绍了剔除数据绑定到数组元素仅工作一半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绑定到数组内部的可观察对象,并且该绑定似乎仅在第一次从模型中读取值时才起作用.随后更改绑定到可观察对象的输入不会更新模型.

I am binding to an observable inside an array and the binding only seems to take affect when reading the value from the model for the first time. Subsequently making changes to the input bound to the observable does not update the model.

JSFiddle在这里: http://jsfiddle.net/coverbeck/hK49f/88/.这是HTML:

A JSFiddle is here: http://jsfiddle.net/coverbeck/hK49f/88/. Here's the HTML:

<div>
    <input data-bind="value: work"/>
    <span data-bind="text: work"></span>
</div>
<!-- ko foreach: nowork -->
<div>
    <input data-bind="value: $data"/>
    <span data-bind="text: $data"></span>
</div>
<!-- /ko -->

和JavaScript:

And the JavaScript:

var viewModel = {
    work: ko.observable('Change me and span changes'), 
    nowork : [ko.observable("Change me and span doesn't update")]
}
ko.applyBindings(viewModel);

在第一个输入中键入内容时,绑定到可观察更新的跨度.当您在第二个输入中键入内容时,绑定到数组内部可观察范围的跨度不会更新.但是输入和跨度正确开始.

When you type something in the first input, the span that is bound to the observable updates. When you type something in the second input, that span that is bound to the observable inside an array does not update. But that input and span start out correctly.

使ko.observableArray而不是普通数组无效"没有区别(我也不希望如此).

Making "nowork" a ko.observableArray instead of a plain array makes no difference (nor would I expect it to).

我觉得我缺少明显的东西,但是我看不到.有人知道发生了什么吗?

I feel like I'm missing something obvious, but I don't see what. Does anybody have an idea what is going on?

谢谢

查尔斯

推荐答案

问题是foreach绑定,在foreach绑定中,您将输入与$data绑定在一起,并且$data属性不是可观察的,而是未包装的可观察的(仅可观察的内容).这就是为什么更改输入字段不会更改您可观察的属性值的原因.

The issue is the foreach binding, in foreach binding you bind the input with $data and $data property is not an observable its an unwrapped observable (only content of an observable). So thats why change to input field is not changing your observable property value.

您应该将输入与可观察的绑定.这是一个工作的小提琴

You should bind the input with observable. Here's a working fiddle

http://jsfiddle.net/w7WQh/

这篇关于剔除数据绑定到数组元素仅工作一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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