0

HTML 数据绑定设置器有问题。我希望它设置为模型(exerciseCategories)间隔值。如果我绑定到模型的间隔,它是正确的值,但不可观察。如果我将它绑定到 $parent.intervals 它是 viewModel 的默认值 (1) 但它是可观察的。我想要两个:)。我究竟做错了什么?像这样的东西确实有效,但显示 [object Object]:

<td data-bind='with: exercise'>
   <input data-bind='value: $parent.intervals(intervals)' />
</td>

What I've got is - HTML    
            ...
            <td>
                <select data-bind='options: exerciseCategories , optionsText: "category", optionsCaption: "Izberite...", value: exerciseType'></select>
            </td>
            <td data-bind="with: exerciseType">
                <select data-bind='options: exercises, optionsText: "title", optionsCaption: "Izberite...", value: $parent.exercise'></select>
            </td>
            <td data-bind='with: exercise'>
                    <input data-bind='value: $parent.intervals' />
            </td>
            ...
 JavaScript
    var exerciseCategories = [
    {
        exercises: [{
            title: 'Aerobic exercise #1',
            intervals: 2
        }],
        category: 'Aerobics'
    }];

        var Exercise = function () {
                var self = this;

                self.exerciseType = ko.observable();
                self.exercise = ko.observable();
                self.intervals = ko.observable(1);
            };
4

1 回答 1

0

执行 $parent.intervals(intervals) 您正在调用 interval 可观察函数,将 interval 作为参数传递,显然您将收到 ko.observable 对象。

我得到了你的摘录。看看这个http://jsfiddle.net/MhHc4/

HTML

Categories:
<select data-bind='options: exerciseCategories , optionsText: "category", optionsCaption: "Izberite...", value: selectedCategory'></select>
<p>selectedCategory() debug: <pre data-bind="text: selectedCategory() ? ko.toJSON(selectedCategory().exercises, null, 2) : ''"></pre>
</p>Exercises:
<select data-bind='options: selectedCategory() ? selectedCategory().exercises : [], optionsText: "title", value: selectedExercise'></select>
<p>selectedExercise() debug: <pre data-bind="text: selectedExercise() ? ko.toJSON(selectedExercise(), null, 2) : 'x'"></pre>
</p>
<input type="text" data-bind="attr : { value : selectedExercise() ? selectedExercise().intervals : 0 }"/>

Javascript

var exerciseCategories = [{
    exercises: [{
        title: 'Aerobic exercise #1',
        intervals: 2
    }],
    category: 'Aerobics'
}];

var ExerciseViewModel = function () {
    var self = this;

    self.exerciseCategories = ko.observable(exerciseCategories);
    self.selectedCategory = ko.observable();
    self.selectedExercise = ko.observable();
    self.intervals = ko.observable(1);
};
ko.applyBindings(new ExerciseViewModel());

高温高压

于 2013-03-04T14:56:29.367 回答