ค้นหาเว็บไซต์

เรียกใช้เมธอด TypeScript จาก jquery ใน Angular 2


ฉันกำลังใช้ bootstrap-select Dropdown ในฟอร์ม Angular 2 กับ jQuery ในเหตุการณ์ onchange ของ jQuery ฉันต้องการเรียกใช้เมธอด onDropDownChangeChange ของ TypeScript แต่มันไม่ทำงาน

import { Component, OnInit, ViewChild } from '@angular/core';
import { RequestOptions } from '@angular/http';
import 'rxjs/add/observable/throw';
declare var $: JQueryStatic;

export class TestComponent implements OnInit {
  selectedValue: string;

  ngOnInit(): void {

        $(function() {
         var self = this;
          $('.selectpicker').on('change', function(){
            var selectedds = $(this).find('option:selected').val();
            self.onDropDownChangeChange(selectedds); //This is not working
            //this.onChange();
          });

        });  

  }

  onDropDownChangeChange(value: string) {
        this.selectedValue = value;
        this.PopulateGrid(selectedValue);
   }


}

คำตอบ #1

คุณกำลังสับสนเกี่ยวกับขอบเขต มันควรจะเป็นแบบนี้:

ngOnInit(): void {

    var self = this;
    $(function() {

          $('.selectpicker').on('change', function(){

              var selectedds = $(this).find('option:selected').val();
              self.onDropDownChangeChange(selectedds); //This is not working
              //this.onChange();

           }); 

    }); 
}

บทความที่เกี่ยวข้อง