【问题标题】:Knockout Computed with Parameter not updated未更新参数计算的淘汰赛
【发布时间】:2014-06-30 19:03:52
【问题描述】:

在 MVC 应用程序中即时从数据库生成多个下拉列表:

<select data-bind="options: findGroup(1).items(),
                        optionsText: 'country',
                        optionsValue: 'id',
                        value: selectedItem(1),
                        event: { change: selectionChange }"></select>

我需要在我的代码中选择当前,但是为了调试目的,我使用跨度:

<span data-bind="text: 'Computed Selected Country: ' + selectedItem(1).country"></span><br />

findgroup(x) 和 selectedItem(x) 是我的 ViewModel 中的全局函数,而所有下拉菜单的函数都是一样的。

selectedItem(x) 应该返回当前选择的下拉选项。 selectedItem(x) 是一个返回计算出的可观察到的淘汰赛的函数。 selectedItem(x) 总是返回“未定义”,不知道为什么......

完整示例:http://jsfiddle.net/LGveR/17/

TIA, 保罗

【问题讨论】:

    标签: javascript jquery knockout.js


    【解决方案1】:

    您的函数返回一个计算值,因此它需要像这样执行,

    http://jsfiddle.net/LGveR/18/

    this.selectedItem(1)().country
    

    【讨论】:

    • 太好了,谢谢,很明显,但被忽略了:)
    • 顺便说一句,每次选择更改时创建一个新的计算结果并不是很像 KO。计算应跟踪更改,但不应重新创建
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    相关资源
    最近更新 更多