[英]knockout js foreach binding displays [object object]
I want to add an <option>
for each item in my _areas array of type KnockoutObservableArray<string>
. 我想为KnockoutObservableArray<string>
类型的_areas数组中的每个项目添加一个<option>
KnockoutObservableArray<string>
。 However when I do this: 但是,当我这样做时:
<select
multiple="multiple"
class="select-multiple"
data-bind="foreach: _regionGetter._areas">
<option data-bind="text: $data"></option>
</select>
It prints out an [object object]
inside each <option>
. 它在每个<option>
内打印出一个[object object]
。 How do I get it to print the string in the array instead of [object object]? 如何获取它以在数组而不是[object object]中打印字符串?
This is because the items in your observable array are objects and then you are using $data
so [object object]
will be displayed. 这是因为可观察数组中的项目是对象,然后您使用$data
以便显示[object object]
。 $data can only be used in this instance if the items were strings. 如果项目是字符串,则只能在此实例中使用$ data。
Instead change <option data-bind="text: $data"></option>
to use the object property that contains the text that you want to be displayed. 而是更改<option data-bind="text: $data"></option>
以使用包含要显示的文本的对象属性。
For example if the objects had a property called name
then you would bind the options text to this by using <option data-bind="text: name"></option>
例如,如果对象具有名为name
的属性,则可以使用<option data-bind="text: name"></option>
将选项文本绑定到此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.