【问题标题】:KnockoutJS data-bind setterKnockoutJS 数据绑定设置器
【发布时间】:2013-02-18 10:28:57
【问题描述】:

HTML 数据绑定设置器有问题。我希望它设置为 model(exerciseCategories) intervals 值。 如果我绑定到模型的间隔,它是正确的值,但不可观察。 如果我将它绑定到 $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);
            };

【问题讨论】:

  • 您如何将数据导入您的视图模型?
  • 您必须提供更多代码。我缺少带有 for: 绑定的 table 标记和您正在绑定的视图模型。我认为您正在混淆视图模型。
  • 这只是摘录。整体与jsfiddle.net/rniemeyer/adNuR 几乎相同

标签: knockout.js


【解决方案1】:

执行 $parent.intervals(intervals) 时,您正在调用区间可观察函数,将区间作为参数传递,显然您将收到 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());

HTH

【讨论】:

  • 这不是我想要的。如果你把 放在最后就是默认值。
猜你喜欢
  • 1970-01-01
  • 2013-12-22
  • 2016-01-13
  • 2014-11-24
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 2015-02-26
相关资源
最近更新 更多