TypeScript / Angular:找不到姓名' XXX'

时间:2017-05-06 23:44:43

标签: function angular typescript components

enter image description here

我在同一个TypeScript组件中有两个函数。当我尝试调用已经声明的内容时,VSCode会报告它" [ts]找不到名称' XXX'。"。

根据Tiep Phan的要求,这是完整的代码:

liveSearchFMs(input: any) {
  this._ectmService.getFMsFromUserSearch(input).subscribe(
    fanmissions => this.fanmissions = fanmissions,
    error => this.errorMessage = <any>error
  );
}

timeout(input) {
  var enteredValue = input;
  var timeout = null;
  clearTimeout(timeout);
  timeout = setTimeout(function () {
    this.liveSearchFMs(enteredValue);
  }, 1000);
}

2 个答案:

答案 0 :(得分:1)

我猜你想创造像这样的东西

export class EctmListComponent implements OnInit {
    // other code
    private timeoutTracker;

    timeout(input) {
        if (this.timeoutTracker) {
            clearTimeout(this.timeoutTracker);
        }
        //use arrow function instead
        this.timeoutTracker = setTimeout(() => {
            this.liveSearchFMs(input);
        }, 1000);
        // or store context first
        /*
        const ctx = this;
        this.timeoutTracker = setTimeout(function() {
            ctx.liveSearchFMs(input);
        }, 1000);
        */
        // or using bind method
        /*
        this.timeoutTracker = setTimeout((function() {
            this.liveSearchFMs(input);
        }).bind(this), 1000);
        */
    }
}

答案 1 :(得分:0)

您需要使用此关键字。所以this.liveSearchFMs