Angular2和jQuery:如何调用组件级功能?

时间:2017-07-19 22:28:55

标签: jquery angular semantic-ui

Angular2和jQuery组合令我困惑。请帮忙! 当我调用弹出对话框和onApprove案例时,我想调用Component的级别function test(),但是它会导致运行时错误:

"TypeError: this.test is not a function"

感谢任何建议!

import { Component, OnInit } from '@angular/core';
declare var $: any;

    enter code here

@Component({
  selector: 'app-schedule',
  templateUrl: './schedule.component.html',
  styleUrls: ['./schedule.component.less']
})

export class ScheduleComponent implements OnInit {

  scheduled : boolean;
  constructor() {
    this.scheduled = false;
   }

  ngOnInit() {
  }

  test() {
    console.log('hello');
  }

  deleteConfirmDlg() {
    $('.mini.modal')
      .modal({
        closable  : false,
        onDeny    : function(){
          window.alert('Wait not yet!');
          return false;
        },
        onApprove : function() {
          this.test();
        }
      }).modal('show');
  }
}

1 个答案:

答案 0 :(得分:3)

您应该使用右箭头操作符来访问this

deleteConfirmDlg() {
 $('.mini.modal')
  .modal({
    closable  : false,
    onDeny    : function(){
      window.alert('Wait not yet!');
      return false;
    },
    onApprove : () => {
      this.test();
    }
  }).modal('show');
 }
}