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

How can a borderless text field be created using Material UI?

Today, I delved into using Material UI for my React project. My goal is to create a registration form similar to this one. Essentially, I want 5 input text fields without any borders and with a simple background color effect when active. However, I'm ...

Display a specific section of an image as the background in a div, with the image scaled and positioned perfectly

Utilizing a 1900 x 1080 image within a div as the background <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <style> html,body { height:100%; } #imageHolder{ ...

The usage of the bootstrapTable() function creates a gap below the displayed table information

Currently, I am working on incorporating a table into my webpage that will load data from an API. After some research, I found a Bootstrap table library to assist with this task. However, I have encountered an issue with setting the table height dynamicall ...

Creating a CSS navigation sub menu

I am having trouble with my CSS code regarding the sub navigation menu. It seems to close as soon as I try to select an option. I suspect that there might be something wrong with the last CSS style. Currently, it only shows when you hover over it but disap ...

Yet again faced with an annoying gap between table rows and cells, for the 532nd instance

Once again, tables have defeated me. I am struggling with a simple table: <table style="width: 200px; margin: 20px auto; border-collapse: collapse; border-spacing: 0; border: none;"> <tr style="margin: 0; padding: 0; border: none; border-colla ...

What is the best way to center text vertically within an image?

How can I vertically align a blog entry title on an entry thumbnail image? The image size changes with the window size and the title varies in length for each entry. After finding a website that successfully achieved this effect, I tried replicating it us ...

The issue of ExpressionChangedAfterItHasBeenCheckedError is a common problem faced by Angular

I have implemented a component loading and an interceptor to handle all requests in my application. The loading component is displayed on the screen until the request is completed. However, I am encountering an error whenever the component inside my router ...

Enhancing the Search Form Minimum Width in Bootstrap 4 Navbar

Looking for a way to create a search form with a width of 100% and a minimum width within a Bootstrap 4 navbar? Check out my code snippet here. <nav class="navbar navbar-expand-md navbar-light bg-faded"> <a href="/" class="navbar-brand">Brand& ...

The Body and HTML elements compress to sizes significantly smaller than the viewport

I am currently working on making my WordPress website responsive. On one of the pages, I have two images that I want to set a max-width of 100% to ensure they are responsive. However, when I shrink the page in Chrome dev tools, I noticed that the <html& ...

Style the labels on the axis of Frappe Charts with colors (potentially utilizing the appropriate CSS selector)

Is it possible to style the x and y axis labels in a Frappe chart with different colors? https://i.stack.imgur.com/A3vUq.png While trying to identify the CSS selectors using Chrome DevTools, I found that a single text element (representing an x axis labe ...

Strip the CSS styling from an image (the CSS being included in the generated code)

I need to remove the CSS styling from an image, but I can't modify the generated code where it's coming from. My actual code looks like this: <div class="bubble"> <img id="one" src="/static/webupload/MyronArtifacts/images/descarga.png ...

Issue with the top margin of the body

Recently, I've encountered a peculiar problem with a standard website layout that I use. The issue seems to be centered around the space between the top and the first div: navbar. No matter what I try, I just can't seem to remove it, as the reas ...

What is the best way to eliminate the outer gutter in a 3-column grid using Bootstrap 3?

Check out this fiddle for more information. http://jsfiddle.net/Nb5C7/ I am curious about the blue gutter on the ends, which seems to be caused by auto margin. How can I get rid of it? In another example provided here , I was able to remove the gutter u ...

Tips for ensuring a static html element remains visible at the bottom of the screen even when the soft keyboard is opened in iOS Safari

Within a webpage, there is an input field and a fixed div positioned at the bottom of the window using CSS properties such as position:fixed; and bottom:0;. To better illustrate this setup, I have created a Codepen demo which can be viewed here: https://c ...

Elements on the left side are not properly aligned

I've successfully coded the head banner and news containers below the header. However, I'm facing an issue with my left menus: Instead of aligning properly with the header and news containers, they are overlapping them. Here is the HTML Code: ...

Blending a background image with CSS

I've set a background image for the div, which is also used for responsive design. Check out the demo My goal is to ensure that the image doesn't appear cut off in the responsive view. I'm looking for a CSS solution to blend the image wit ...

What is the best way to display a responsive website design exclusively for certain devices?

My website currently lacks responsiveness. Typically, we can check if a website is responsive by resizing the browser to see if it adjusts using CSS3 media queries. However, I am looking to create a better user experience by ensuring that visitors using a ...

What is the best way to access dropdown sub-menu options from a complex multi-level navigation bar

Struggling to figure out how to open my dropdown sub-menu using CSS. Hoping to make it so the user can open it by hovering over the corresponding tag. I attempted to use #lablinksDD to trigger the opening of #ddSbMenu when hovered over #menuDD with #labLin ...

Changing the CSS background in React when updates occur

Currently working on toggling the CSS background image within this React application. The images have been successfully imported and a variable called "image" is set to the imported image as the iteration increases/decreases with each click. The issue I&a ...

How can I implement a hover-over image change effect similar to the one seen in Gmail accounts?

I am attempting to implement a feature that allows users to change their image, similar to what can be done in a GMail account. When the user hovers over the image, an option to "change image" should appear. This may be a new concept for me because I am ...