如何在Angular 2中制作此jQuery事件处理程序? [英] How to make this jQuery event handler in Angular 2?
问题描述
我正在Angular 2中创建一个Web应用程序.在其中一个页面上,我使用了一个名为 jquery-comments ,以启用用户评论.
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.
现在,如果用户单击jquery-comments答复"按钮,我想显示一个自定义对话框.
Now I'd like to show a custom dialog box, if the user clicks the jquery-comments "Reply" button.
如果是jQuery,我会做类似的事情:
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
});
但是在不更改jquery-comments插件的情况下,如何在Angular 2中创建类似的事件处理程序?
But how would I make a similar event handler in Angular 2, without changing the jquery-comments plugin?
推荐答案
在角度组件中,您应该从 使用@ViewChild()模板,在初始化视图之后 可以使用该对象的nativeElement属性并传递给jQuery.
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.
将$(或jQuery)声明为JQueryStatic将为您提供类型 引用jQuery.
Declaring $ (or jQuery) as JQueryStatic will give you a typed reference to jQuery.
参考: https://stackoverflow.com/a/30662773/510788
将jQuery注入组件后,您可以捕获点击.
You can catch the click after injecting jQuery into your component.
这篇关于如何在Angular 2中制作此jQuery事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!