淘汰赛绑定不会使用简单的可观察对象数组进行更新 [英] Knockout binding doesn't update using array of simple observables

查看:69
本文介绍了淘汰赛绑定不会使用简单的可观察对象数组进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型中有一个字符串列表.要编辑它们,我希望每个显示为带有文本框的<li>和删除该项目的<button>.因此,对于列表['A', 'B'],我想要这样的东西:

I have a list of strings in my view model. To edit them, I want each to appear as an <li> with a textbox, and a <button> to remove the item. So, for the list ['A', 'B'], I want something like this:

<ul data-bind="foreach: titles">
    <li>
        <input value="A" data-bind="value:$data" />
        <button data-bind="click: $root.remove">remove</button>
    </li>
    <li>
        <input value="B" data-bind="value:$data" />
        <button data-bind="click: $root.remove">remove</button>
    </li>
</ul>
<button data-bind="click: add">add</button>

我可以最初创建它,但是无法获取更新的值以反映在视图模型中,并且无法使删除按钮正常工作.

I can create that initially, but am not getting updates to the values to reflect in the view model, and can't get the remove buttons working.

我最初使用的是纯字符串的observableArray,然后更新为observable字符串的observableArray.使用纯字符串,可以使用删除"按钮,但是可以预见的是,它没有更新视图模型.

I initially had an observableArray of plain strings, then updated to an observableArray of observable strings. With the plain strings, the remove button worked, but it, predictably, didn't update the view model.

我已经设置了一个JS小提琴,其问题相当孤立: http://jsfiddle .net/bdukes/uvyH3/2/

I've setup a JS fiddle with the issue fairly isolated: http://jsfiddle.net/bdukes/uvyH3/2/

如果有确定的方法或更好的方法,我很想知道.

If there's an established or better way of doing this, I'd love to know.

此外,作为一个不相关(但不太重要)的问题,stringifyJson实用程序似乎总是为我提供数组中每个项目的空结果.

Also, as an unrelated (and less important) issue, the stringifyJson utility always seems to give me empty results for each item in the array.

推荐答案

Knockout当前不适用于一系列纯可观察对象(已记录问题

Knockout does not currently work well with an array of pure observables (issue logged here).

要使其正常工作,您需要将您的商品设置为具有可观察对象的对象,例如:

To make this work properly, you need your items to be objects that hold observables like:

{ val: ko.observable("something") }

以下是您的拨弄小提琴,可以使用以下类型的对象: http://jsfiddle.net/rniemeyer/GgFa9/

Here is your fiddle updated to use these type of objects: http://jsfiddle.net/rniemeyer/GgFa9/

这篇关于淘汰赛绑定不会使用简单的可观察对象数组进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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