简体   繁体   English

可观察元素的数组未更新

[英]Array of observable elements is not updating

I have observable array with some objects inside of it. 我有一些对象的可观察数组。 One of the properties of this object is an array of observable elements (dates in a string format). 该对象的属性之一是可观察元素的数组(日期以字符串格式)。 Here is a small reproducible example: 这是一个可重现的小示例:

this.groups = ko.observableArray([{
    name:       ko.observable("name"),
    deadlines:  [
        ko.observable("2010-02-08"),
        ko.observable("2013-06-18"),
        ko.observable("2015-01-23"),
    ]
}]);

I am representing them in the following format: 我以以下格式表示他们:

<tbody data-bind="foreach: groups">
        <tr>
            <td>
                <input type="text" placeholder="name" data-bind="value: name"><br><br><br>
                <span data-bind="text: name"></span>
            </td>
            <!-- ko foreach: deadlines -->
            <td>
                <input type="date" data-bind="value: $data"><br><br><br>
                <span data-bind="text: $data"></span>
            </td>
            <!-- /ko -->
        </tr>
    </tbody>

The problem is that when I am updating one of the dates, they are not updating. 问题是,当我更新日期之一时,它们没有更新。 Here is a JSfiddle example : as you see when 2010-02-08 is updated the underlying text stays the same. 这是一个JSfiddle示例 :正如您在2010-02-08更新时所看到的,底层文本保持不变。

At the beginning I thought that it has something to do with date in a binding, but after trying this I see that it is not the case. 起初我还以为它是与日期有约束力的,但在尝试后这个我看到它是不是这样的。 Any idea of what went wrong? 有什么问题的主意吗?

Bind to the $rawData instead. 而是绑定到$rawData

<!-- ko foreach: deadlines -->
<td>
    <input type="date" data-bind="value: $rawData"><br><br><br>
    <span data-bind="text: $data"></span>
</td>
<!-- /ko -->

Items of arrays are always unwrapped, so you were effectively binding to the value of the observable, not the observable itself. 数组项始终是未包装的,因此您实际上是绑定到可观察对象的值,而不是可观察对象本身。 The $rawData variable gives you access to the not-unwrapped item. $rawData变量使您可以访问未展开的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM