我是TypeScript的新手,并试图弄清楚为什么我的代码没有按预期工作 .
我在HTML视图中的按钮上有一个单击绑定 . changeDateFormatted函数应该简单地将dateFormatted字符串变量的文本更改为“123” . 我的代码按预期更新了我的测试变量,但对于dateFormatted变量,我收到以下错误:
_this.invoice(...) . dateFormatted不是函数
我无法弄清楚我是否可以或应该在嵌套类中更改observable的属性 .
这是我的代码:
import * as ko from 'knockout';
import 'isomorphic-fetch';
interface Item {
name: KnockoutObservable<string>;
itemNumber: KnockoutObservable<string>;
amount: KnockoutObservable<number>;
unitPrice: KnockoutObservable<number>;
}
interface Invoice {
dateFormatted: KnockoutObservable<string>;
invoiceNumber: KnockoutObservable<string>;
items: KnockoutObservableArray<Item>;
}
class InvoiceViewModel {
public invoice: KnockoutObservable<Invoice> = ko.observable<Invoice>();
public test: KnockoutObservable<string> = ko.observable<string>("test");
constructor() {
fetch('api/SampleData/GetInvoice')
.then(response => response.json() as Promise<Invoice>)
.then(data => {
this.invoice(data);
});
}
public changeDateFormatted = () => {
this.test('123');
this.invoice().dateFormatted('123');
}
}
export default { viewModel: InvoiceViewModel, template:
require('./invoice.html') };