在控制台中敲除可观察的更新但不绑定

时间:2013-05-08 07:23:45

标签: model-view-controller knockout.js

我似乎无法确定此代码的问题而且我正在挣扎。希望有更好的Javascript排骨的人可以看到我做错了什么。 Chrome控制台每秒都会显示更新值,但它不会在视图中绑定。我在http://jsfiddle.net/PLSMW/7/

有一个jsfiddle

html绑定只是:

js文件:

function viewModel() {
    var self = this;

    self.BigDay = new Date("03 Jun 2013, 14:30:00");
    self.msPerDay = 24 * 60 * 60 * 1000;
    self.countdown = ko.observable("24:00:00:00");

    self.tick = function () {

        var today = new Date();
        var timeLeft = (self.BigDay.getTime() - today.getTime());

        var e_daysLeft = timeLeft / self.msPerDay;
        var daysLeft = Math.floor(e_daysLeft);

        var e_hrsLeft = (e_daysLeft - daysLeft) * 24;
        var hrsLeft = Math.floor(e_hrsLeft);

        var e_minsLeft = (e_hrsLeft - hrsLeft) * 60;
        var minsLeft = Math.floor(e_minsLeft);

        var e_secsLeft = (e_minsLeft - minsLeft) * 60;
        var secsLeft = Math.floor(e_secsLeft);
        self.countdown = daysLeft + " : " + hrsLeft + " : " + minsLeft + " : " + secsLeft;
        console.log(self.countdown);
    };
    setInterval(self.tick, 1000);
}
ko.applyBindings(new viewModel());

2 个答案:

答案 0 :(得分:4)

observable是function,因此您无法将其直接设置为更新后的值。相反,你必须调用它:

self.countdown(daysLeft + " : " + hrsLeft + " : " + minsLeft + " : " + secsLeft);

要在控制台中显示它,你应该

console.log(self.countdown());

答案 1 :(得分:-2)