简体   繁体   English

剔除selectedOption计算为无效

[英]knockout selectedOption as computed not working

I've got a weird problem that doesn't make any sense to me. 我有一个奇怪的问题,对我来说没有任何意义。

The code below will show the drop down correctly, but the selectedOptions doesn't work for reading. 下面的代码将正确显示下拉列表,但selectedOptions无法读取。 The read function does get called but the default of 3 isn't being set. 确实调用了read函数,但未设置默认值3。

The write function definitely works though. 写功能当然可以。

If I use fChosenAnswer instead, the default is correctly being set with #3 being preselected. 如果我改用fChosenAnswer,则默认设置为正确设置,并且已预先选择#3。

html: 的HTML:

 <!-- this one doesn't have a default but it should be defaulting to 3 -->
<select class="example" data-bind="options: AvailableAnswersArray, selectedOptions: ChosenAnswer"></select>

<!-- this one does correctly default to 3 -->
<select class="example2" data-bind="options: AvailableAnswersArray, selectedOptions: fChosenAnswer"></select>

in the viewmodel: 在视图模型中:

this.fChosenAnswer = ko.observableArray([3]);

this.ChosenAnswer = ko.computed({
    read: function () {
        console.log("ChosenAnswer read function is hit but the drop down doesn't show it as selected."); //this log shows in the console
        return ko.observableArray([3]);
    },
    write: function (value) {
        console.log("updated chosen answer to ", value);
        // this log shows in the console
    },
    owner: this
});

Any ideas? 有任何想法吗?

Thanks to Adrian, the answer was to just return [3] instead of an observableArray. 感谢Adrian,答案是仅返回[3]而不是observableArray。

read: function () {
    return [3];
},

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

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