Is it possible to implement a feature in Angular and Bootstrap where the toggle menu can be closed by clicking anywhere on the page, rather than just the toggle button

I'm working on an Angular project where I've implemented a navbar component. The navbar is responsive and includes a toggle button that appears when the browser window is resized. This button allows users to hide or display the menus.

One issue I'm facing is that currently, clicking anywhere other than the button does not close the displayed menus. I would like it so that clicking anywhere outside the button automatically closes the menus in this Angular Project component.

 <div class="row my-row"> 
    <div class="col my-col-1 ">
        <input type="checkbox" id="menu" />
            <label for="menu" class="menu">
                <span></span>
                <span></span>
                <span></span>
            </label>
            <nav class="nav text-white">
                <ul>
                    <li><a routerLink=""><i  class="bi bi-house my-fas"></i> <span class="my-span text-white">&nbsp; Home</span></a></li>
                    <li><a (click)="onClick('feature')" ><i class="bi bi-play my-fas"></i> <span class="my-span">&nbsp; Featured Games</span></a></li>
                    <li><a (click)="onClick('quickPlay')"><i class="bi bi-play my-fas"></i> <span class="my-span">&nbsp; Most Played Games</span> </a></li>
                    <ng-template  [ngIf]="permission">
                        <!-- <li ><a href="#"><i class="fas fa-user my-fas"></i> <span class="my-span">&nbsp; Profile</span></a></li> -->
                        <li (click)="onLogout()"><a href="#"><i class="bi bi-power my-fas"></i> <span class="my-span text-white">&nbsp; Logout</span></a></li>
                        <li (click)="onLogout()"><a href="#"><i class="bi bi-person-x my-fas"></i> <span class="my-span text-white">&nbsp; Unsubscribe</span></a></li>
                    </ng-template>
                    
                </ul>
            </nav>
    </div>
    <div class="col my-col-2">
          <div *ngIf="allow ; else loggedOut">
            <span class="user-name">{{userName}}</span>
            <img src="../../../assets/Avatar/1.png" alt="Avatar" class="avatar">
            
            <div class="dropdown dropleft">
              <span type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="drop-down" > <i style="color: white;" class="ml-4 bi bi-caret-down"></i></span>
              <div class="dropdown-menu " style="margin-right: -50px;margin-top:40px;border-radius: 8px;" aria-labelledby="dropdownMenuButton">
                <!-- <a class="dropdown-item" href="#"><i class="fas fa-user my-fas"></i> <span class="my-span">&nbsp; Profile</span></a> -->
                <a  class="dropdown-item" href="http://gamenow.noeticworld.com/unsub-portal"><i class="bi bi-person-x my-fas"></i> <span class="my-span">&nbsp; Unsubscribe</span></a>
                <a (click)="onLogout()" class="dropdown-item" href="#"><i class="bi bi-power my-fas"></i> <span class="my-span">&nbsp; Logout</span></a>
              </div>
            </div>

          
          </div>
          <ng-template #loggedOut>
            <button style="outline: none;" (click)="onClick('login')" class="my-button">
                Login
            </button>
            <button style="outline: none;" (click)="onClick('login')" class="my-button">
                  Join Now
            </button>
          </ng-template>
    </div>
    
  </div>

Answer №1

To add the desired feature, consider implementing Host listeners within your menu component as shown below:

@HostListener('document:click')
handleClick() {
   // Implement code to toggle the menu using a reference
} 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unexpectedly, the jQuery get function retrieves the entire webpage

I'm encountering difficulties when using .get requests to update elements through page load or button press. Here is the code snippet... $(document).ready(function() { //updateVariable(); $('#dannychoolink').html('<?php dan ...

Encountering an issue: Integrating JSON data into HTML using Angular.JS!

Having trouble transferring JSON data to HTML using Angular.JS for a programming course. The author doesn't seem to have this issue, can anyone provide some assistance? I've correctly linked Angular and added the app, but it's not working a ...

Clicking a button in VueJs will trigger the clearing of the displayed text and the subsequent execution of

Hello, I am new to the world of web development and I'm currently learning vue.js. I am working on an app where I input an ID and upon clicking the search button, a method is called. This method utilizes axios to make a request to the controller and ...

Is it possible to implement GrailsApplicationCommand within an angular project using the gradle wrapper?

Within my Grails angular project, I have a unique GrailsApplicationCommand that I need to execute in the context of the server project. Currently, I am able to successfully run this command from the working directory of the server using the grails CLI: g ...

Anchors that need to be clicked multiple times before activating

'I recently encountered a strange bug. My CSS3 anchors/buttons, which I have been simplifying by removing properties, sometimes require multiple clicks to function. This is completely new to me and quite perplexing. The issue seems to occur randomly, ...

A curious phenomenon observed in the behavior of the jQuery offset() method

Once I executed the following code snippet multiple times: $view.offset({ left : X, //X remains constant top : this.y }); console.log($view.offset()); //displays expected output I noticed (using Firebug) that the HTML code looked like this: <di ...

What imports are needed for utilizing Rx.Observable in Angular 6?

My goal is to incorporate the following code snippet: var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: { lat: -25.363, lng: 131.044 } }); var source = Rx.Observable.fromEventPattern( function (han ...

What is the recommended approach for testing a different branch of a return guard using jest?

My code testing tool, Jest, is indicating that I have only covered 50% of the branches in my return statement test. How can I go about testing the alternate branches? The code snippet below defines a function called getClient. It returns a collection h ...

Storing an array of objects in local storage is not working in Angular 9

I've encountered an issue trying to save an array of JSON objects into local storage, and I'm puzzled as to why it's not functioning correctly. Despite utilizing localStorage.setItem('comparisons', JSON.stringify(setComparisons)), ...

Ways to showcase INPUT TYPE when making a Selection?

I've been struggling with a simple issue and despite trying multiple solutions, I can't seem to get it right. I have a form where I'm using the <select> tag with two options: coo and uh. What I want is for an additional input type fiel ...

Switching slides in Ionic 4 with a simple button click

I want to switch slides by clicking a button on my presentation. Here is an example code snippet: <ion-slides> <ion-slide> Slide one <ion-slide> <ion-slide> Slide Two <ion-slide> </ion-slides> <butt ...

Display a text box upon clicking an item

I've been curious about how to create the effect of a text box or div that appears when clicked on, similar to what you see in this. Scroll down and click on "show patchnotes"; I've been puzzled by this for a while but haven't come across a ...

Tips for correctly displaying diacritics with Webpack and Typescript

While working on my project, I encountered an issue with diacritics marks (such as German or Polish characters) when using Webpack with Typescript. Unfortunately, the problem arises when trying to display these marks in the console or on a webpage. It seem ...

Angular Error: Unable to access property 'users' on a null value

I am working on a component that takes in data through the @Input() decorator regarding a group. My objective is to generate a new array of objects during the initialization of the component based on the data from the group array. Below is the TypeScript c ...

Having issues with Tailwind classes not being applied properly on dynamically generated pages in Gatsby-node

Currently, I am working on building dynamic pages using gatsby-node. The templates for these pages are stored in the templates/ directory. However, I have run into an issue where I cannot utilize tailwind classes within these templates unless they are al ...

What could be causing this addEventListener to fail when I am assigning elements to a class?

I've encountered an issue with my code where I have two text inputs and two date inputs. I tried to select all of them using QuerySelectorAll with a class, added a click listener that should change the textContent of a div element to "", but it's ...

Tool for tracking response time on basic Ajax-requested webpage

Looking for an easy way to use ajax to load content onto a page. I want the loaded content to wait before appearing on the page, but using jQuery's .delay() function didn't work as expected. Any suggestions on how to achieve this? Appreciate any ...

Issue: The key length and initialization vector length are incorrect when using the AES-256-CBC encryption algorithm

Within my coding project, I have developed two essential functions that utilize the AES-256-CBC encryption and decryption algorithm: import * as crypto from "crypto"; export const encrypt = (text: string, key: string, iv: string) => { con ...

Ways to expose a declared module in Enzyme's shallow rendering?

I've been grappling with how to ensure that my custom declared module is detected when I shallow render a component using enzyme in Jest unit tests. The issue revolves around a specific module declaration named _aphrodite, which looks like this: // i ...

Embed a video within an image while ensuring it remains responsive on all devices

I have a picture that resembles something like this one My goal is to embed a video into it while maintaining its aspect ratio when the screen size is reduced. The responsive design should only affect the width since the image will be displayed in portrai ...