Reputation: 35703
I am getting the error Uncaught TypeError: Object #<Object> has no method 'getInvoices'
when I call this.getInvoices
in the ajax.error result. How can I access the typescript function from there?
// 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?
}
});
}
}
Upvotes: 0
Views: 5034
Reputation: 944
Use short typescript functions syntax, it captures class context by default:
// Typescript
class InvoicesController {
...
public getInvoices(skip: number, take: number): void {
...
}
public createInvoice() {
$.ajax({
...
contentType: 'application/json',
type: 'POST',
success: (res) => {
if (res.result === 'ok') {
this.getInvoices(0,100); // WORK NOW
}
},
error: (err) => {
this.getInvoices(0,100); // WORK NOW
}
});
}
}
Upvotes: 1
Reputation: 70
check your scope. I believe when you are calling this you're actually referring to the ajax object and not the class 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);
}
});
}
Upvotes: 5