javascript - 在 Javascript 函数中调用 Typescript 函数

标签 javascript this typescript

我收到错误 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/

相关文章:

javascript - 将一个组件的值传递给另一个 Angular

javascript - Angular 延迟加载 - 没有看到它正在工作

javascript - 如何在回调中访问正确的“this”?

javascript - 如何从 JavaScript 中的匿名事件监听器访问对象属性

typescript - 如何获取复杂类型以在 vue.js 模块中初始化和声明数据

javascript - AmCharts 基于数据的动态列

javascript - V8 中的 ES6 箭头函数词法 this

javascript - HTML5 FileList 和 JQuery 各自

javascript - Chrome 扩展程序 - 不保存数据

javascript - 显示或隐藏 Div