简体   繁体   中英

KnockOut.JS optionsText

I have this code:

self.Groups = ko.observableArray();
function LoginNameObject(name) {
    this.loginName = name;
}

self.Groups.push(new LoginNameObject('Blah'));

I am trying to bind in the html like this:

 <select data-bind="options: Groups, optionsText: loginName"></select>

but keep on getting loginName is undefined when it tries to bind. Any help would be appreciated!

Thanks

In the optionsText you need to specify your property name is a string .

So you need to write

<select data-bind="options: Groups, optionsText: 'loginName'"></select>

See also in the documentation: Example 3: Drop-down list representing arbitrary JavaScript objects, not just strings

Alternatively if you need a more complex logic to calculate the option text you can also specify the optiosText as a function:

<select 
   data-bind="options: Groups, optionsText: function(item) { return item.loginName }">
</select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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