我是 KnockoutJS 新手。
我在下面写了一个视图,在运行时看不到每个类别的 categoryVariables。你能看看这里有什么问题吗?
谢谢你的帮助。
我的代码视图:
<table class="table">
<thead>
</thead>
<tbody data-bind="foreach: categories">
<tr>
<td data-bind="text: Name">
<table>
<tbody data-bind="foreach: categoryVariables">
<tr>
<td data-bind="text: Name"></td>
</tr>
</tbody>
</table>
</td>
<td><button data-bind='click: $root.addCategoryVariable'>Add a category variable</button></td>
</tr>
</tbody>
</table>
<button data-bind='click: addCategory'>Add a category</button>
<script type="text/javascript">
var resumeDataViewModel;
function Category(data) {
var self = this;
self.ID = data.ID;
self.Name = data.Name;
self.categoryVariables = ko.observableArray([
new CategoryVariable({ID: '1', Name: 'asd'})
]);
}
function CategoryVariable(data) {
var self = this;
self.ID = data.ID;
self.Name = data.Name;
}
function ResumeDataViewModel() {
var self = this;
self.categories = ko.observableArray([
new Category({ID: '1', Name : 'Contact', Category: {ID: '1', Name: 'gfdg'}}),
new Category({ID: '2', Name : 'Education', Category: {ID: '2', Name: 'asdasd'}})
]);
self.addCategory = function(){
self.categories.push(new Category({
Name: "sa d"
}));
}
self.addCategoryVariable = function (category) {
category.categoryVariables.push(new CategoryVariable({
Name: 'asd'
}));
}
}
ko.applyBindings(resumeDataViewModel = new ResumeDataViewModel());
</script>
正在寻找您的回复。非常感谢。