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);
   }
}
                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();
           }); 
    }); 
}
                        You're missing the context.
And to handle 3rd party library custom event, in some case you need tell Angular run your code after received the event like this.
constructor(private zone: NgZone) {}
ngOnInit(): void {
    var self = this;
    this.zone.run(() => {
      $('.selectpicker').on('change', function(){
        var selectedds = $(this).find('option:selected').val();
        self.onDropDownChangeChange(selectedds);
        //this.onChange();
      }); 
    });
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With