【发布时间】:2013-05-17 01:03:29
【问题描述】:
我是打字稿的新手,想将它与淘汰赛的优点结合起来。我有一个计算的 observable,目前可以工作,但想知道这是正确的方法还是有更好的方法。
我正在使用knockout definition file from nu-get。其中有 4 个 KnockoutComputed(x) 定义。
- 淘汰赛计算
- KnockoutComputedDefine
- KnockoutComputedFunctions
- KnockoutComputedStatic
我喜欢声明 observable 的 {} 方法,并希望保留它。长话短说,这是声明 observables 的正确方法还是有另一种方法(可能在函数中使用 intlisense)
我是这样使用第一个的:
class PersonViewModel {
public firstname: KnockoutObservable<string>;
public lastname: KnockoutObservable<string>;
public fullname: KnockoutComputed<string>;
constructor() {
this.firstname = ko.observable('');
this.lastname = ko.observable('');
this.fullname = ko.computed({
owner: this,
read: function () {
return this.firstname() + " " + this.lastname();
}
});
}
}
用html sn-p:
<h2>Type Script and Knockout.</h2>
<input data-bind="value: firstname" />
<input data-bind="value: lastname" />
<div data-bind="text: fullname"></div>
【问题讨论】: