敲除计算的可观察写入不更新
Knoockout computed observable write doesnt update
以下是计算的可观察对象,我调用了它的写入函数,但写入不会更新计算的值。
self.pagesToBeDisplayed = ko.computed({
read: function () {
return self.pages();
}, write: function (totalCount) {
self.pages(totalCount)
},
deferEvaluation: true
});
我将上面的可观察对象称为
self.pagesToBeDisplayed(5)
。但是,只有 self.pages
的值被更新并且
self.pagesToBeDisplayed
仍然是旧值。
它按预期工作:
var viewmodel = function(){
var self = this;
self.pages = ko.observable(2);
self.pagesToBeDisplayed = ko.computed({
read: function () {
return self.pages();
}, write: function (totalCount) {
self.pages(totalCount)
},
deferEvaluation: true
});
self.update = function(){
self.pagesToBeDisplayed(5);
};
};
ko.applyBindings(new viewmodel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
Update via input:
<input type="number" data-bind="value: pagesToBeDisplayed">
<br>
Update via JS:
<button data-bind="click: update">Update</button>
<br><br>
Latest values:
<br>
self.pages:<span data-bind="text: pages"></span>
<br>
self.pagesToBeDisplayed:<span data-bind="text: pagesToBeDisplayed"></span>
可能是您代码的其他部分导致了问题。
以下是计算的可观察对象,我调用了它的写入函数,但写入不会更新计算的值。
self.pagesToBeDisplayed = ko.computed({
read: function () {
return self.pages();
}, write: function (totalCount) {
self.pages(totalCount)
},
deferEvaluation: true
});
我将上面的可观察对象称为
self.pagesToBeDisplayed(5)
。但是,只有 self.pages
的值被更新并且
self.pagesToBeDisplayed
仍然是旧值。
它按预期工作:
var viewmodel = function(){
var self = this;
self.pages = ko.observable(2);
self.pagesToBeDisplayed = ko.computed({
read: function () {
return self.pages();
}, write: function (totalCount) {
self.pages(totalCount)
},
deferEvaluation: true
});
self.update = function(){
self.pagesToBeDisplayed(5);
};
};
ko.applyBindings(new viewmodel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
Update via input:
<input type="number" data-bind="value: pagesToBeDisplayed">
<br>
Update via JS:
<button data-bind="click: update">Update</button>
<br><br>
Latest values:
<br>
self.pages:<span data-bind="text: pages"></span>
<br>
self.pagesToBeDisplayed:<span data-bind="text: pagesToBeDisplayed"></span>
可能是您代码的其他部分导致了问题。