I'm working with Angular 8 and when I press a button need to execute a javaScript function but that doesn't work.
I have a method that I found here how to import external javaScript.
The objetive of the button is to hide a navbar.
I have the code like this:
import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery'
@Component({
selector: 'app-home-admin',
templateUrl: './home-admin.component.html',
styleUrls: [
'./home-admin.component.css',
'./Style/style.css'
]
})
export class HomeAdminComponent implements OnInit {
constructor() { }
ngOnInit(): void {
this.loadScript('https://code.jquery.com/jquery-1.12.0.min.js');
this.loadScript('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js');
this.loadScript('https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.concat.min.js');
}
public loadScript(url: string) {
const body = <HTMLDivElement>document.body;
const script = document.createElement('script');
script.innerHTML = '';
script.src = url;
script.async = false;
script.defer = true;
body.appendChild(script);
}
click($event) {
$('#sidebarCollapse').on('click', function () {
$('#sidebar, #content').toggleClass('active');
$('.collapse.in').toggleClass('in');
$('a[aria-expanded=true]').attr('aria-expanded', 'false');
});
}
}
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Scrollbar Custom CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.css">
</head>
<body>
<div class="wrapper">
<nav id="sidebar">
<div class="sidebar-header">
<h3 align="left">CHROMAE PROJECT</h3>
</div>
</nav>
<div id="content">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button class="btn btn-info navbar-btn" type="submit" (click)="click($event)">
<i class="glyphicon glyphicon-align-left"></i>
<span>MENÚ</span>
</button>
</div>
</div>
</nav>
</div>
</div>
</body>
</html>
I try to put the function into the html but it doesn't work.
Can anyone help me?