简体   繁体   English

使用Knockout将optionsText绑定到包含对象数组的属性?

[英]Bind optionsText to property with an array of objects using Knockout?

I have an array of objects stored inside an observableArray in Knockout.js, and I'm trying to bind one of the properties of the objects to the optionsText binding for a select element, however, this isn't working an the options are not being displayed. 我有一个存储内部对象的数组observableArray在Knockout.js,和我想要绑定的对象来的属性之一optionsText具有约束力select元素,但是,这是行不通的选项是不被显示。

The observableArray is initially blank and is populated by a AJAX request: observableArray最初是空白的,并由AJAX请求填充:

self.currentPeople = ko.observableArray([]);

Following the AJAX request, I can console.log this and receive the following back: 在AJAX请求之后,我可以在console.log查看并收到以下内容:

console.log(self.currentPeople);
// Produces [Object, Object] where each of the objects have properties of `personId` and `personName`

Yet my select dropdown remains unpopulated: 然而,我的选择下拉列表仍未填充:

<select class="large-3" data-bind="options: currentPeople, optionsText: 'personName', optionsValue: 'personId', optionsCaption: 'All',  value: currentPerson"></select>

Only the 'All' appears. 只显示“全部”。 Any ideas? 有任何想法吗?

If console.log(self.currentPeople) shows your array of objects, that means you're (incorrectly) populating it using assignment instead of calling it as a function. 如果console.log(self.currentPeople)显示您的对象数组,则意味着您(错误地)使用赋值填充它而不是将其作为函数调用。

Populate it as: 将其填充为:

self.currentPeople(newData);

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

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