打字稿中的函数无法在alertify.js

时间:2017-03-20 02:34:32

标签: jquery angular typescript alertifyjs

我在打字稿中使用alertify插件,但无法识别getData功能。请参阅以下代码

copyTemplate(id:any, pluginId:any, name:any ) {
     alertify.confirm(`Are you sure you want to copy ${name} to a new project template?`, function () {
        this.getData();
     }, function() {
          (<HTMLInputElement>document.getElementById('prefGroup')).value = '0';
     });
}

它出了什么问题?浏览器出错:

  

core.umd.js:3064 EXCEPTION:this.getData不是   functionErrorHandler.handleError @ core.umd.js:3064next @   core.umd.js:8039schedulerFn @   core.umd.js:3689SafeSubscriber .__ tryOrUnsub @   Subscriber.ts:238SafeSubscriber.next @   Subscriber.ts:190Subscriber._next @ Subscriber.ts:135Subscriber.next @   Subscriber.ts:95Subject.next @ Subject.ts:61EventEmitter.emit @   core.umd.js:3675NgZone.triggerError @ core.umd.js:4038onHandleError @   core.umd.js:3999ZoneDelegate.handleError @   zone.js?1489977130473:207Zone.runTask @   zone.js?1489977130473:139ZoneTask.invoke @ zone.js?1489977130473:304   core.umd.js:3069 ORIGINAL STACKTRACE:ErrorHandler.handleError @   core.umd.js:3069next @ core.umd.js:8039schedulerFn @   core.umd.js:3689SafeSubscriber .__ tryOrUnsub @   Subscriber.ts:238SafeSubscriber.next @   Subscriber.ts:190Subscriber._next @ Subscriber.ts:135Subscriber.next @   Subscriber.ts:95Subject.next @ Subject.ts:61EventEmitter.emit @   core.umd.js:3675NgZone.triggerError @ core.umd.js:4038onHandleError @   core.umd.js:3999ZoneDelegate.handleError @   zone.js?1489977130473:207Zone.runTask @   zone.js?1489977130473:139ZoneTask.invoke @ zone.js?1489977130473:304   core.umd.js:3070 TypeError:this.getData不是函数       在Object.eval [as onOkay](project-templates.component.ts:126)       在HTMLButtonElement。 (alertify.js 1489977130519:280)       在ZoneDelegate.invokeTask(zone.js?1489977130473:236)       at Object.onInvokeTask(core.umd.js:3969)       在ZoneDelegate.invokeTask(zone.js?1489977130473:235)       在Zone.runTask(zone.js?1489977130473:136)       在HTMLButtonElement.ZoneTask.invoke(zone.js?1489977130473:304)ErrorHandler.handleError @   core.umd.js:3070next @ core.umd.js:8039schedulerFn @   core.umd.js:3689SafeSubscriber .__ tryOrUnsub @   Subscriber.ts:238SafeSubscriber.next @   Subscriber.ts:190Subscriber._next @ Subscriber.ts:135Subscriber.next @   Subscriber.ts:95Subject.next @ Subject.ts:61EventEmitter.emit @   core.umd.js:3675NgZone.triggerError @ core.umd.js:4038onHandleError @   core.umd.js:3999ZoneDelegate.handleError @   zone.js?1489977130473:207Zone.runTask @   zone.js?1489977130473:139ZoneTask.invoke @ zone.js?1489977130473:304   Subscriber.ts:241 Uncaught TypeError:this.getData不是函数

1 个答案:

答案 0 :(得分:3)

使用箭头功能。 &#34;这个&#34;的范围在回调中是不同的

copyTemplate(id:any, pluginId:any, name:any ) {
 alertify.confirm('Are you sure you want to copy ${name} to a new project template?',  () => {
    this.getData();
 }, () => {
      (<HTMLInputElement>document.getElementById('prefGroup')).value = '0';
 });

}

或将此值存储在函数外部并使用它

copyTemplate(id:any, pluginId:any, name:any ) {
 let self = this;
 alertify.confirm('Are you sure you want to copy ${name} to a new project template?',  function() {
    self.getData();
 }, function() {
      (<HTMLInputElement>document.getElementById('prefGroup')).value = '0';
 });

}