视图未更新 - css class 缺失
view does not update - css class missing
当我按下“更新”按钮时,我看到了具有正确值 (true/false) 的警报,但是 class“完成”不会得到应用。
为什么视图不更新?
<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;
}
};
;
更改 getter & setter 方法。
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) { //getter
alert("true")
self.complete(false); //setter
}else {
alert("false")
self.complete(true); //setter
}
};
}
var viewModel = function() {
var self = this;
self.steps = ko.observableArray();
self.steps.push(new Step('ID', 'TEST', 'TEST'));
return self;
}
ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<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>
当我按下“更新”按钮时,我看到了具有正确值 (true/false) 的警报,但是 class“完成”不会得到应用。 为什么视图不更新?
<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;
}
};
;
更改 getter & setter 方法。
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) { //getter
alert("true")
self.complete(false); //setter
}else {
alert("false")
self.complete(true); //setter
}
};
}
var viewModel = function() {
var self = this;
self.steps = ko.observableArray();
self.steps.push(new Step('ID', 'TEST', 'TEST'));
return self;
}
ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<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>