以下是计算得出的可观察对象,并且我正在调用其write函数,但写入操作不会更新计算出的值。
self.pagesToBeDisplayed = ko.computed({
read: function () {
return self.pages();
}, write: function (totalCount) {
self.pages(totalCount)
},
deferEvaluation: true
});
我称上述可观察为
self.pagesToBeDisplayed(5)
。但是,只有self.pages
的值会更新,
self.pagesToBeDisplayed
仍然是较旧的值。
答案 0 :(得分:0)
它按预期工作:
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>
可能是代码的其他部分导致了问题。