0

I added the JQuery-UI MonthPicker to my Angular 6 application.
I want to trigger an Angular action when my date changes:

  ngOnInit() {
    $(document).ready(function() {
      $('#myMonthPicker').MonthPicker(
        {
          Button: false,
          MaxMonth: -1,
          MonthFormat: 'MM yy'
          OnAfterChooseMonth: this.update()
        }
      );
    });
  }

  update() {
    alert('works');
  }

The update method is never hit.
And I have the following error:

TypeError: this.update is not a function

How could I call an Angular method from my JQuery-UI code ?

1 Answer 1

1

Because the this scope has been lost by using function() {}, try:

ngOnInit() {
  const update = this.update();

  $(document).ready(function() {
    $('#myMonthPicker').MonthPicker(
      {
        Button: false,
        MaxMonth: -1,
        MonthFormat: 'MM yy'
        OnAfterChooseMonth: update
      }
    );
  });
}

Or change your function to ES6 arrow notation:

$(document).ready(() => {
  ...
});

Note: This generally fixes this problem, but I'm not how jQuery UI handles it. Can't guarantee this will work

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, using the ES6 arrow notation allows me to call this.update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.