2

I am creating a web application in Angular 2. On one of the pages, I use a jQuery plugin called jquery-comments, to enable user comments.

Now I'd like to show a custom dialog box, if the user clicks the jquery-comments "Reply" button.

enter image description here

If it was jQuery, I would do something like:

$('.jquery-comments .reply').on('click', function (event) {
    event.preventDefault();
    ...here I can show my custom dialog box or something else        
});

But how would I make a similar event handler in Angular 2, without changing the jquery-comments plugin?

Matthew Green
  • 10,161
  • 4
  • 36
  • 54
brinch
  • 2,544
  • 7
  • 33
  • 55

2 Answers2

1

In your angular component you should reference a DOM element from the template using @ViewChild() After the view has been initialized you can use the nativeElement property of this object and pass to jQuery.

Declaring $ (or jQuery) as JQueryStatic will give you a typed reference to jQuery.

reference: https://stackoverflow.com/a/30662773/510788

You can catch the click after injecting jQuery into your component.

Community
  • 1
  • 1
Kalmár Gábor
  • 434
  • 4
  • 10
0

You can attach an event handler click with Angular like so...

Some Component HTML

<div class="reply" (click)="showComments($event)"></div>

Passing $event will give you the same event object that you would get by just passing event with vanilla js.

Some Component

    class someComponent {

      constructor() {
    }

    showComments(e) {
      e.preventDefault();
      // do the jquery stuff here.
    }

    }
  • No that won't work. As I wrote, I don't want to change the code of the jquery-comments plugin, which I would have to, if I were to add a (click) event handler as you suggest. – brinch Feb 08 '17 at 22:08
  • I don't think I am understanding what you mean by change the jquery plugin code. – Garrett Sanderson Feb 08 '17 at 22:14
  • The "Reply" button is part of the jquery-comment plugin. If I should do
    , I would need to do this inside the plugin.
    – brinch Feb 08 '17 at 22:18