代码位于typescript中,我试图在页面加载中显示当前日期时间,并在刷新点击时更新该日期时间。刷新函数中的Knockout Observable字符串更新
在.ts文件中显示日期时间的字符串是这样声明的。
asOfString: KnockoutObservable<string>;
在它被设置这样的构造,
this.asOfString = ko.observable(new Date().toDateString() + " " + new Date().toLocaleTimeString());
在HTML中它是这样的绑定,
<a class="small button" data-bind="click: updateSummary">Refresh</a>
在刷新按钮,我试图更新它像这个,
this.asOfString(new Date().toDateString() + " " + new Date().toLocaleTimeString()); //latest date time.
it gi给我一个错误,_this.asOfString不是一个函数。
请帮忙。视图模型的
代码是这样的,
import ko = require('knockout');
class TodayViewModel {
asOfString: KnockoutObservable<string>;
constructor() {
this.updateSummary();
this.asOfString = ko.observable(new Date().toDateString() + " " + new Date().toLocaleTimeString());
}
updateSummary =() => { // is the function that is bound to refresh button
//want to update this.asOfString here
}
}
export = TodayViewModel;
我们需要看到更多的代码,当你更新它(得到的'价值this'是什么的想法)。发布您如何定义视图模型,如何在视图模型上定义asOfString,以及如何在视图模型上定义updateSummary。此外,确保'asOfString'不被任何可观察的东西覆盖。 –
听起来像一个错误的'this'。在点击绑定中使用的'updateSummary'函数将(默认情况下)** not **与您的视图模型一起调用,因为'this'' – CrimsonChris
已更新问题。 –