【发布时间】:2015-09-15 02:01:47
【问题描述】:
我正在尝试在 knockoutjs 中创建一个可写的可计算 observable,我几乎完成了,但它无法读取计算值。难道我做错了什么?
this.time_remaining = ko.computed({
read: function() {
return this.time_remaining; <!-- Does this need setInterval? -->
},
write: function() {
window.setInterval(function() {
this.time_remaining = moment('2015-09-14').countdown().toString();
console.log(this.time_remaining)
<!-- The console shows a timer, that is exciting! -->
}, 1000)
},
owner: this
});
但是,它仍然无法读取数据绑定中的值,并且无法正常工作。也许我做错了。
<h4 data-bind="text: time_remaining" class="timer"></h4>
【问题讨论】:
-
您需要另一个属性作为支持变量。现在看起来您将计算值作为值返回,并且写入覆盖了计算值。
标签: knockout.js