我收到错误 Uncaught TypeError: Object #<Object> has no method 'getInvoices'
当我调用this.getInvoices
时在 ajax.error 结果中。我如何从那里访问 typescript 功能?
// Typescript
class InvoicesController {
...
public getInvoices(skip: number, take: number): void {
...
}
public createInvoice() {
$.ajax({
...
contentType: 'application/json',
type: 'POST',
success: function (res) {
if (res.result === 'ok') {
this.getInvoices(0,100); // THIS DOES NOT WORK?
}
},
error: function (err) {
this.getInvoices(0,100); // THIS DOES NOT WORK?
}
});
}
}
最佳答案
检查你的范围。我相信当您调用它时,您实际上指的是 ajax 对象而不是 InvoicesController 类
public createInvoice() {
me = this;
$.ajax({
....
contentType: 'application/json',
type: 'POST',
success: function (res) {
if (res.result === 'ok') {
console.log('Data saved1');
}
else {
console.log('Save error1');
}
},
error: function (err) {
me.getInvoices(100,0); // TRY THIS
console.log("error2"+err);
}
});
}
关于javascript - 在 Javascript 函数中调用 Typescript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19009277/