用独立变量敲除foreach绑定

knock out foreach binding with independent varriable

本文关键字:foreach 绑定 独立 变量      更新时间:2023-09-26

我在我的html5应用程序中使用knockout进行绑定。

我有一个奇怪的场景。

我正在绑定的一个div用于下面的循环

<div data-bind="foreach: oneList">
<select name="dropDown1" id="dropDown1" data-bind="options: ddList,optionsText: function(item) { return item.value;},optionsValue:function(item) { return item.key; }">
</select>
<input type="text" id="newValue" data-bind="value : oneValue"/>
</div>

这里oneList是微分变量,ddList是微分变量。

因此,当实际绑定发生时,下拉列表不会被绑定,但输入文本被绑定,因为oneList.oneValue是有效的,但oneList.ddList不是有效的

如果我的问题不清楚,请告诉我

如果看不到视图模型,很难判断,但很可能在oneList列表中的项上没有定义ddList属性。

foreach绑定中,当前绑定上下文指的是列表中的当前项,因此如果需要在绑定上下文中"向上"访问与onlist处于同一级别的属性,则需要使用$parent(或$root访问主视图模型)。

固定的options绑定如下所示:

<select data-bind="options: $parent.ddList, optionsText:...  " >