【问题标题】:Why is my ui not updating?为什么我的ui没有更新?
【发布时间】:2018-03-13 04:58:01
【问题描述】:

当我尝试add items 进行用户输入时:

// Here's my data model
var ViewModel = function() {
    var self = this;
    self.concentration = ko.observable();
    self.calibrations = ko.observableArray();
    self.sampleMeasure = ko.observable();

    self.add = function() {
    debugger;
        self.calibrations.push({
            x: undefined,
            yT: undefined,
            yM: undefined
        });
    };

    self.remove = function() {
        self.calibrations.remove(this);
    };

    self.testing = function() {
        var data = [
            { x: 2, yT: 5.5, yM: 5.3 },
            { x: 6, yT: 13.5, yM: 13.2 },
            { x: 8, yT: 17.5, yM: 17.2 },
            { x: 10, yT: 21.5, yM: 21.6 },
            { x: 14, yT: 29.5, yM: 29.3 },
            { x: 19, yT: 39.5, yM: 39.6 }
        ];

        ko.utils.arrayPushAll(data, self.calibrations);

        self.calculateSampleConcentration();
    };

    self.calculateSampleConcentration = function() {
    debugger;
        self.concentration = 5;
    };

    self.testing();
};

ko.applyBindings(new ViewModel()); // This makes Knockout get to work

我尝试添加 debugger; 语句以查看发生了什么,但它没有显示源代码在那里中断,尽管它显示处理器已停止。

它还一直显示以下错误:

{"error": "Please use POST request"}

即使我删除了围绕我的输入的表单元素。

【问题讨论】:

    标签: jquery debugging knockout.js data-binding jsfiddle


    【解决方案1】:

    你不是正确的binding click events:

    应该是:

    <button data-bind="click: add">Add Calibration</button>
    

    remove 按钮也应更改为:

    <button data-bind="click: $parent.remove">Remove</button>
    

    删除功能仍然不起作用。应该改为:

    self.remove = function(item) {
       // "item" argument will have the current calibration item being removed
       self.calibrations.remove(item);
    };
    

    这是updated fiddle。您需要按照相同的方式更改其他按钮。

    【讨论】:

    • 谢谢,你知道如何调试代码吗?别介意我以前(现已删除)的问题,那是由于参数顺序,道歉。
    • @MrFox 您可以在函数中添加debugger;。但是您必须在 jsfiddle 中保持开发工具打开(按 F12)。它对你不起作用,因为函数 ws 由于语法错误而从未调用过。
    猜你喜欢
    • 1970-01-01
    • 2018-03-21
    • 2011-10-10
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多