[英]Knockout JS Cannot bind to an array
I am trying to bind array to a table, so it shows all my array contents.我正在尝试将数组绑定到一个表,所以它显示了我所有的数组内容。
I tried first example, which works (purely in HTML):我尝试了第一个示例,该示例有效(仅在 HTML 中):
<table>
<thead>
<tr><th>First name</th><th>Last name</th></tr>
</thead>
<tbody data-bind="foreach: people">
<tr>
<td data-bind="text: firstName"></td>
<td data-bind="text: lastName"></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function Model() {
this.people = [
{ firstName: 'Bert', lastName: 'Bertington' },
{ firstName: 'Charles', lastName: 'Charlesforth' },
{ firstName: 'Denise', lastName: 'Dentiste' }
];
}
ko.applyBindings(new Model());
</script>
Then I got to the next level, and tried bigger example, which always shows error然后我进入了下一个级别,并尝试了更大的示例,它总是显示错误
Unable to process binding "foreach: function(){return interests }" Message: Anonymous template defined, but no template content was provided
无法处理绑定“foreach: function(){return Interests}”消息:定义了匿名模板,但未提供模板内容
Below is faulty code:下面是错误代码:
// Activates knockout.js when document is loaded. window.onload = (event) => { ko.applyBindings(new AppViewModel()); } // This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI function AppViewModel() { this.firstName = ko.observable("Bert"); this.lastName = ko.observable("Bertington"); this.fullName = ko.computed(() => this.firstName() + " " + this.lastName(), this); this.interests = ko.observableArray([ { name: "sport" }, { name: "games" }, { name: "books" }, { name: "movies" } ]); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <table> <thead> <tr> <th>Interest</th> </tr> </thead> <tbody data-bind="foreach: interests"></tbody> <tr> <td data-bind="text: name"></td> </tr> </table>
I tired already with regular array, but with no luck.我已经厌倦了常规阵列,但没有运气。
You are closing the <tbody>
before the inner template:您正在关闭内部模板之前的
<tbody>
:
<tr>
<td data-bind="text: name"></td>
</tr>
So, the tr
is now not in the context of the foreach
binding.因此,
tr
现在不在foreach
绑定的上下文中。
Move the </tbody>
to after the </tr>
and before the </table>
tags:将
</tbody>
移动到</tr>
之后和</table>
标记之前:
// Activates knockout.js when document is loaded. window.onload = (event) => { ko.applyBindings(new AppViewModel()); } // This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI function AppViewModel() { this.firstName = ko.observable("Bert"); this.lastName = ko.observable("Bertington"); this.fullName = ko.computed(() => this.firstName() + " " + this.lastName(), this); this.interests = ko.observableArray([ { name: "sport" }, { name: "games" }, { name: "books" }, { name: "movies" } ]); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <table> <thead> <tr> <th>Interest</th> </tr> </thead> <tbody data-bind="foreach: interests"> <tr> <td data-bind="text: name"></td> </tr> </tbody> <!-- close here --> </table>
Change your HTML to将您的 HTML 更改为
<table>
<thead>
<tr>
<th>Interest</th>
</tr>
</thead>
<tbody>
<tr data-bind="foreach: interests">
<td data-bind="text: name"></td>
</tr></tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.