knockout.js 视图不更新-缺少css类

ghhkc1vu  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(180)

当我按下“更新”按钮时,我看到了带有正确值(真/假)的警告,但类“完成”没有得到应用。为什么视图没有更新?

<div data-bind="foreach: steps">
    <div data-bind="css: {done: complete}">
        <span data-bind="text: name"></span>
        <button data-bind="click: updateStatus">Update</button>
    </div>
</div>

var viewModel = function() {
    var self = this;
    self.steps = ko.observableArray();

    loadTestData(testDataSteps);
    function loadTestData(stepsArray) {
        $.each( stepsArray, function( index, value ){
            self.steps.push(new Step(value.id, value.name, value.step))
        });
    }
}

var Step = function (id, name, step){
    var self = this;
    self.id = ko.observable(id);
    self.name = ko.observable(name);
    self.step = ko.observable(step);
    self.complete = ko.observable(false);

    self.updateStatus = function () {
        if(self.complete === true) {
            alert("true")
            self.complete = false;
        }else {
            alert("false")
            self.complete = true;
        }
    };
;

相关问题