简体   繁体   English

选择列表上的KnockoutJs绑定问题

[英]KnockoutJs binding issue on select list

I'm using Typescript with KnockoutJs and I am having issues with binding the optionsText and optionsValue. 我在KnockoutJs中使用Typescript,但是在绑定optionsText和optionsValue时遇到问题。 The model is: 该模型是:

 export interface LanguageProxy {
    ID: number;
    Name: string;
    Code: string;
    IsSparse: boolean;
    HasAudio: boolean;
    ReadsRightToLeft: boolean;
    IsAsian: boolean;
    ShortCode: string;
    LongCode: string;
    CultureCode: string;
    IsEnabled: boolean;
    IsCustom: boolean;
}

we are setting up the binding as (response being a response from a web service call): 我们将绑定设置为(响应是来自Web服务调用的响应):

var langs = ko.observableArray([]);
        response.LanguageProxyListResult.forEach(lang => {
            langs.push(ko.observable(lang));
        });

        this.Languages = langs;
        ko.applyBindings(this, jQuery("#QuickSearchContainer")[0]);

and we are binding using the following HTML below: 并且我们使用以下HTML进行绑定:

<select name="ddlLanguage" id="ddlLanguage" class="LanguageList"  
                                data-bind="options: Languages,
                                            optionsText: 'Name',
                                            optionsValue: 'ID',
                                            optionsCaption: 'Choose...',
                                            optionsAfterRender: function (e) { jQuery('#ddlLanguage')[0].selectedIndex = 1;}">

                            </select>

The data binds correctly, removing the optionsText and optionsValue returns the list of [object] [OBJECT], but when adding the properties of optionsText and value it sets up a blank list. 数据正确绑定,删除optionsText和optionsValue会返回[object] [OBJECT]的列表,但是在添加optionsText和value的属性时会设置一个空白列表。

Looking at a knockoutJs context debugger for chrome, the data appears correctly in the element (under $data.Languages.Symbol(_latestValue) and the parsed context) . 查看chrome的淘汰赛上下文调试器,数据正确显示在元素中(在$ data.Languages.Symbol(_latestValue)和解析的上下文下)。 Is there something fundamentally I am doing wrong? 从根本上说我有做错什么吗?

I don't think the options binding supports observables in the array. 我不认为options绑定支持数组中的可观察对象。

If you replace langs.push(ko.observable(lang)); 如果您替换langs.push(ko.observable(lang)); by just langs.push(lang); 通过仅langs.push(lang); , it should work. ,它应该可以工作。

There's no real point in wrapping an object in an observable when it's in an observable array. 当对象位于可观察数组中时,将对象包装在可观察对象中没有任何实质意义。

Reproduction of problem, note that the second select will throw an error. 重现问题,请注意第二个select将引发错误。

 var opts = ko.observableArray([ { name: "Option 1" }, { name: "Option 2" } ]); var obsObs = ko.observableArray([ ko.observable({ name: "Option 1" }), ko.observable({ name: "Option 2" }) ]); ko.applyBindings({ opts: opts, obsObs: obsObs }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <select data-bind="options: opts, optionsText: 'name'"></select> <select data-bind="options: obsOpts, optionsText: 'name'"></select> 

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

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