exercises
我在 observableArray中有一个数组workouts
。我想添加从中删除项目的功能。试图使它成为一个 observableArray:
self.exercises = ko.observableArray();
并添加了删除项目的功能:
self.removeExercise = function() {
self.exercises.remove(this);
};
但什么也没有发生。虽然相同的函数适用于workouts
父数组。除此之外,我还有一个用于编辑数组中项目的简单函数,如下所示:
this.edit = function() { this.editing(true) }
并尝试在我的视图中绑定它:
<span data-bind="text: name, click: edit"> </span>
但它不起作用。问题可能出在哪里?
这是我的看法
<div class="content">
<ul data-bind="foreach: workouts">
<li>
<span data-bind="text: name, click: edit"> </span>
<a href="#" data-bind="click: $parent.removeWorkout">Remove</a>
<ul data-bind="foreach: exercises">
<li>
<span data-bind="text: name"> </span>
<a href="#" data-bind="click: $parent.removeExercise">remove</a>
</li>
</ul>
</li>
</ul>
</div>
和视图模型:
function AppViewModel() {
var self = this;
self.workouts = ko.observableArray([
{name: "Workout1", exercises:[
{ name: "Exercise1.1" },
{ name: "Exercise1.2" },
{ name: "Exercise1.3" }
]},
]);
self.exercises = ko.observableArray();
self.removeWorkout = function() {
self.workouts.remove(this);
};
self.removeExercise = function() {
self.exercises.remove(this);
};
this.editing = ko.observable(false);
this.edit = function() { this.editing(true) }
}
ko.applyBindings(new AppViewModel);
这是关于 jsfiddle 的示例:http: //jsfiddle.net/c9fQB/
谢谢!