I'm creating a click event to document that close my aside menu, I created example in jquery but i don't want to do this in jquery and I cannot access to 'menu' variable.
How can I write this in pure angular ?
import { Component, HostListener, ElementRef } from "@angular/core";
import { Router } from "../../node_modules/@angular/router";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
})
export class AppComponent {
menu: boolean;
date = new Date();
@HostListener('document:click', ['$event'])
clickout(event) {
if(this.eRef.nativeElement.contains(event.target)) {
this.menu = false;
} else {
console.log('clicked outside');
}
}
constructor(public router: Router,
private eRef: ElementRef) {}
showMenu() {
this.menu = !this.menu;
}
}
EDIT I edited code from duplicate, but I have different idea
Is there other HostListener to ignore aside and button with id ? Or am I thinking wrong ?
When i click on aside or button menu this clickout event must not working, when i click anywhere else it should set me this.menu to false.
<aside id="aside" [hidden]="!menu">
<button class="menu-btn" (click)="menu = false" routerLink="/">Strona Główna</button>
<button (click)="menu = false" routerLink="/cv">CV</button>
<button (click)="menu = false" routerLink="/bio">BIO</button>
<button (click)="menu = false" routerLink="/portfolio">Portfolio</button>
<button (click)="menu = false" routerLink="/kontakt">Kontakt</button>
</aside>
<button id="menu-btn" [class.aside]="menu" (click)="showMenu()">Klik</button>