使用挖空将选项文本绑定到具有对象数组的属性

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

本文关键字:对象 数组 属性 绑定 文本 选项      更新时间:2023-09-26

我在 Knockout .js 的observableArray中存储了一个对象数组,我正在尝试将对象的一个属性绑定到 select 元素的optionsText绑定,但是,这不起作用,因为选项没有显示。

observableArray 最初是空白的,由 AJAX 请求填充:

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

在 AJAX 请求之后,我可以console.log这个并收到以下回复:

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

然而,我的选择下拉列表仍未填充:

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

仅显示"全部"。有什么想法吗?

如果console.log(self.currentPeople)显示你的对象数组,这意味着你(错误地)使用赋值填充它,而不是将其作为函数调用。

将其填充为:

self.currentPeople(newData);