简体   繁体   English

外部 JS 文件单击事件在 angular 8 中不起作用

[英]External JS file click event is not working in angular 8

I am learning Angular 8 and I came across one issue.我正在学习 Angular 8,但遇到了一个问题。 I have downloaded one HTML template and I am integrating that template to angular.我已经下载了一个 HTML 模板,我正在将该模板集成到 angular。

While clicking on button.在单击按钮时。 It is expanding and displaying menus.它正在扩展和显示菜单。

在此处输入图片说明

在此处输入图片说明

I have js file in assets folder scripts.js.我在资产文件夹scripts.js中有js文件。 And that scripts.js file contains jquery code to open and close this menu.并且该scripts.js 文件包含用于打开和关闭此菜单的jquery 代码。 But after adding html code to html of my component file this click event is not working.但是在将 html 代码添加到我的组件文件的 html 后,此单击事件不起作用。 When I am checking source in browser I can see scripts.js is loaded in browser.当我在浏览器中检查源代码时,我可以看到 scripts.js 已加载到浏览器中。 But I am not sure why it is not calling click event.但我不确定为什么它不调用 click 事件。 Below is html code for click event.下面是点击事件的html代码。

<div class="header-close">
            <svg viewBox="0 0 100 25">
                <path id="curve_close" fill="#B4E3F2" d="M100, 25 C62.5, 25, 62.5, 25, 50, 25 S37.5, 25, 25, 25"
                        data-start-d="M100, 25 C62.5, 25, 62.5, 25, 50, 25 S37.5, 25, 25, 25"
                        data-end-d="M100, 25 C62.5, 25, 62.5, 0,  50, 0  S37.5, 25, 0,  25"/>
            </svg>
            <a href="javascript:;" id="menu-close"><i class="fa fa-close"></i></a>
        </div>


        <div class="header-tool">
            <div class="entry-tool">

                <svg viewBox="0 0 100 25">
                    <path id="curve_open" fill="#B2E2F2" d="M0, 0 C37.5, 0, 37.5, 25, 50, 25 S62.5, 0, 100, 0"
                        data-start-d="M0, 0 C37.5, 0, 37.5, 25, 50, 25 S62.5, 0, 100, 0"
                        data-end-d="M0, 0 C37.5, 0, 37.5, 0, 50, 0 S62.5, 0, 100, 0" />
                </svg>
                <a href="javascript:;" id="menu-handler"><i class="fa fa-bars"></i></a>
            </div>
        </div>

ID for my anchor tag is menu-handler .我的锚标记的 ID 是menu-handler and code written in scripts.js is as below.用scripts.js编写的代码如下。

    // Menu event handler
var toggleMenu = function(close_menu){
    $('#header').find('.entry-header').toggleClass('show-menu');
    $('#header').find('.entry-header').slideToggle();

    $('#menu-handler').hide();
    $('#menu-close').hide();
    setTimeout(function(){
        if( close_menu ){ $('#menu-handler').fadeIn(); }
        else{ $('#menu-close').fadeIn(); }
    }, 800);
};

$('#menu-handler').on('click', function(){
toggleMenu(false);

    TweenMax.to($('#curve_open'), 0.4, {attr: {'d': $('#curve_open').data('end-d')}});
    setTimeout(function(){
        $('.header-close').show();
        $('.header-tool').hide();
        TweenMax.to($('#curve_close'), 0.4, {attr: {'d': $('#curve_close').data('end-d')}});
    }, 400);
});

I am also not getting any error while click on button.单击按钮时我也没有收到任何错误。 I am not sure can we call external js functions directly from component html pages?我不确定我们可以直接从组件 html 页面调用外部 js 函数吗? or I have to use angular events like (click) and then call this.或者我必须使用像 (click) 这样的角度事件,然后调用它。

Try this approach试试这个方法

import '..path../js/external-js-file.js';

declare var externalJsFile: any; // variable as the name of the function inside external-js-file.js

export class MyComponent {

    doSomething() {
        new externalJsFile(); // execute function 
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM