我想要使用foreach和复选框将Knockout observableArray绑定到我的UI,然后根据所检查的内容创建一个数组。KnockoutJS observableArray与模板和foreach
我收到此错误: 未捕获的ReferenceError:无法处理绑定“template:function()。。。”
这里是我的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>
这里是我的淘汰赛视图模型:
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
Q1,Q3
Q1,Q3,Q2
Q1,Q3,Q2,Q4
Q1,Q2,Q4