【发布时间】:2014-04-26 01:51:27
【问题描述】:
我正在尝试使用 foreach 和复选框将 Knockout observableArray 绑定到我的 UI,然后根据检查的内容创建一个数组。
我收到此错误: 未捕获的引用错误:无法处理绑定“模板:函数()......”
这是我的 HTML:
<dl data-bind="template: { name: 'QuarterTemplate', foreach: Quarter, templateOptions: { selections: SelectedQuarters } }"></dl>
<script id="QuarterTemplate" type="text/html">
<dd>
<label>
<input type="checkbox" data-bind="attr: { value: quarter }, checked: $item.selections" />
<a data-bind="text: quarter" ></a>
</label>
</dd>
</script>
这是我的 Knockout ViewModel:
function ViewModel() {
this.Quarter = ko.observableArray([
{ quarter: "Q1" },
{ quarter: "Q2" },
{ quarter: "Q3" },
{ quarter: "Q4" }
]);
this.SelectedQuarters = ko.observableArray();
this.SelectedQuarters.subscribe(function () {
console.log(this.SelectedQuarters());
});
}
$(document).ready(function () {
ko.applyBindings(new ViewModel());
});
我还设置了一个小提琴:
最终我想在控制台中看到的是这样的:
第一季度
第一季度,第三季度
Q1、Q3、Q2
Q1,Q3,Q2,Q4
Q1、Q2、Q4
【问题讨论】:
标签: knockout.js foreach ko.observablearray