我试图弄清楚为什么这个嵌套模板不显示任何内容。我有 2 个类 Foo/Bar,ViewModel 有一个可观察的 Foo 数组,而 Foo 有一个 Bar 的集合
目前我看到的只是 Foo 项目
IE
- 某项
代替
某项
- 子项
项目清单
<ul data-bind="template: {name: 'TopTemplate' , foreach: foos}"></ul>
<script type="text/html" id="TopTemplate">
<li data-bind='text: Name'>
<ul data-bind=" template: {name: 'NestedTemplate' , foreach: bars} " style="list-style-type:circle;margin-left:15px">
</ul>
</li>
</script>
<script type="text/html" id="NestedTemplate">
<li data-bind='text: Name'>
</li>
</script>
var Foo = function () {
var self = this;
self.Name = ko.observable('someitem');
self.bars = ko.observableArray([new Bar()]);
self.HasChildren = ko.observable(false);
self.addBar = function () {
self.bars.push(new Bar());
};
self.removeBar = function (param) {
self.bars.remove(param);
};
self.bars.push(new Bar());
}
var Bar = function () {
var self = this;
self.Name = ko.observable('subitem');
}
var ViewModel = function () {
var self = this;
self.foos = ko.observableArray([new Foo()]);
self.addFoo = function () {
self.foos.push(new Foo());
};
self.removeFoo = function (param) {
self.foos.remove(param);
};
}
$.ajax({
url: '@Url.Action("AddFoos")',
type: 'GET',
async: false,
contentType: 'application/json',
success: function (result) {
ko.applyBindings(new ViewModel(result));
}
});
提前致谢!