我试图弄清楚为什么这个嵌套模板不会显示任何内容。我有2个类Foo / Bar,ViewModel有一个可观察的Foo数组,而Foo有一个Bar的集合
目前我所看到的只是Foo项目
即
而不是
someitem
列出项目
<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));
}
});
提前致谢!