Call TypeScript Method from jquery in angular 2

Prabhu Arumugam picture Prabhu Arumugam · May 9, 2017 · Viewed 10.2k times · Source

Im using boostrap-select Dropdown in angular 2 forms with jquery. Onchange jquery event i want to call a Typescript onDropDownChangeChange method. But its not working.

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);
   }


}

Answer

eko picture eko · May 9, 2017

You are confusing the scopes. It should be like this:

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();

           }); 

    }); 
}