我有这个视图模型
var ViewModel = function (groups) {
var self = this;
self.groups = ko.observableArray(ko.utils.arrayMap(groups, function (group) {
return {
planName: ko.observable(group.Key),
documents: ko.observableArray(ko.utils.arrayMap(group.Values, function(value) {
return {
displayCrudLinks: ko.observable(value.displayCrudLinks),
document: ko.observable(value.Document)
};
}))
};
}));
};
我正在尝试以这种方式使用模板(第二个模板的内部跨度只是检查它是否适用于每个文档,最终我需要绑定到文档本身的属性)。我得到了“planName”,但是它下面没有任何内容,似乎不适用于文档
<script type="text/html" id="documentsTmpl">
<div class="PlanDocuments">
<div data-bind="text: planName" class="PlanName">
<div data-bind="template: { name: 'docFragmentTmpl', foreach: documents }" class="Preview"></div>
</div>
</div>
</script>
<script type="text/html" id="docFragmentTmpl">
<span>Document</span>
</script>
我甚至无法使用单个模板
<script type="text/html" id="documentsTmpl">
<div class="PlanDocuments">
<div data-bind="text: planName" class="PlanName">
<div data-bind="foreach: documents" class="Preview">Document</div>
</div>
</div>
</script>
我传递给映射函数的组具有以下结构@
[{Key: "General", Values: [{DisplayCrudLinks: true, Document: {...bunch of simple properties}}, ...]}, ....]
感谢您的帮助
修改
我忘记了绑定的HTML
<div id="list" data-bind="template: { name: 'documentsTmpl', foreach: groups }"></div>