collapsible menu with expand/collapse button

When I click on the menu, such as the administration section,

step1

Next, I proceed to click on the toggle button

step2

After clicking, the toggle button becomes activated

step3

Then, I click on the button toggle once more

step4

My issue arises when an empty line <li> is inadvertently added.

step5

How can I remove this unwanted empty line?

dashboard.component.html

<div class="sidebar" [ngClass]="{ active: showSideBar }">
    <div class="logo-details">
        <span class="logo_name">
          <img class="logo_dashboard" src="assets/images/logo_FR.gif" style="border-bottom: 1px solid grey">
      </span>
    </div>
    <ul id="accordion" class="accordion">
        <li *ngFor="let menu of menus; let i = index" [class.active]="menu.active">
            <ng-container>
                <div class="menu" (click)="selectMenu(menu)">
                    <i [class]="menu.iconClass"></i> {{ menu.name }}
                    <i class="fa fa-chevron-down"></i>
                </div>
                <ul class="submenu" #submenu [ngStyle]="{ 'height': menu.active ? submenu.scrollHeight + 'px' : 0 + 'px' }">
                    <li *ngFor="let submenu of menu.submenu">
                        <a routerLink="{{ submenu.url }}"> {{ submenu.name }} </a>
                    </li>
                </ul>
            </ng-container>
        </li>
    </ul>
</div>
<section class="home-section">
    <nav>
        <div class="sidebar-button">
            <div class="menu-container">
                <i class="bx bx-menu sidebarBtn" (click)="toggleSideBar()"></i>
            </div>
            <div class="menu-summary-container">
                <span class="user">Utilisateur:  </span>

                <span class="last-connection">La dernière connexion est le :
     
                </span>

            </div>
        </div>
    </nav>
    <router-outlet></router-outlet>
</section>

The code can be accessed here.

Thank you for your assistance.

Answer №1

Don't worry, the li tag is not empty. The height is being set using the following code:

[ngStyle]="{ 'height': menu.active ? submenu.scrollHeight + 'px' : 0 + 'px' }"

This code adds a raw inline style in pixels.

To solve this issue, you have a couple of options. You can simplify your accordion to make it clearer and easier to understand (consider using a pre-built component for simplicity), or you can work with a different property like display instead:

[ngStyle]="{display: menu.active ? 'initial' : 'none'}"

Then, adjust the style and animation as needed.

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

Error: Unable to locate module - The specified file cannot be resolved when utilizing an external JavaScript library

I am currently integrating a WYSIWYG editor (TUI Editor) into my Angular2+ application. Since there is no official Angular wrapper available, I have decided to create my own based on an existing wrapper. Due to some installation issues with npm, I saved t ...

Utilizing D3 to fetch geographic data in the form of a TopoJSON file for U.S. counties

After retrieving a set of coordinates, the goal is to use D3 to find the corresponding county from a U.S. TopoJSON file. Here is an example code snippet: navigator.geolocation.getCurrentPosition(function(position) { let coordinates: [number, number] = [p ...

Issues with routing in AngularJS2 causing navigation problems

My Angularjs 2 routing seems to be malfunctioning, even when I manually enter the URL it still doesn't work. Below are my configuration details and versions: var ngVer = '@2.0.0-rc.5'; // locking in the angular package version var routerV ...

What is the methodology for obtaining the setter property type in TypeScript?

Describe a scenario where an object contains both getter and setter methods with different types. How can we determine the type of the setter function? Consider defining an object with getter and setter functions like the example below: type Foo = { g ...

Opening the Gmail app from a link using JavaScript

What is the best way to open the Gmail app from a hyperlink? This link opens WhatsApp <a href="https://wa.me/">whatsapp</a> <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a1f190f ...

Unable to access any pages using the angular-cli http-server for deployment

When I run my project using 'ng serve', everything works fine. However, when I deploy it on a server using 'http-server ./dist', none of the pages can be accessed and an error message is displayed: host:~/morange/project$ http-server . ...

Why does Angular routerlink display %20 before the id path?

In my quest to showcase messages from a nested collection of messages, I have encountered a peculiar issue. When clicking on the "view" tag within certain cards, I use routerlink to navigate to the intended path where messages are displayed. Strangely en ...

Running the ng serve --o command on Windows cmd results in the command prompt closing

Whenever I execute the command: ng serve --o The process closes abruptly in Windows cmd, preventing me from using ctrl+C to terminate the running angular application. ...

Developing a Generic API Invocation Function

I'm currently working on a function that has the capability to call multiple APIs while providing strong typing for each parameter: api - which represents the name of the API, route - the specific route within the 'api', and params - a JSON ...

Is there a way for me to determine the version of Angular that I am currently using?

Is there a way for me to figure out which version of Angular I am currently using? I attempted the following: angular --version angular --v angular -version angular -v Unfortunately, I received this error: -bash: angular: command not found Based on yeo ...

Using Angular in conjunction with a chrome extension that includes a content script

Currently, I am developing a Chrome extension with Angular to handle styling and routing. My issue lies in the communication between content and background scripts - specifically, when I update a variable in component X using chrome.runtime.onMessage, Angu ...

VS Code fails to identify Typescript internal modules

I am currently facing issues with separating my TypeScript classes into distinct files using internal modules. Unfortunately, the main.ts file is not loading or recognizing the sub-modules. main.ts /// <reference path="Car.ts" /> module Vehicles { ...

Although NgRx retrieves data from the API, it does not save it to the state

I've been struggling to grasp a fundamental concept in implementing ngrx. For instance, I have a situation where I need data from an API to validate information in my component, but once the validation is complete, I no longer need that data stored in ...

Determine if the "type" field is optional or mandatory for the specified input fields in Typescript

I need to determine whether the fields of a typescript type or interface are optional or required. export type Recommendation = { id?: string, name: string, type?: string, tt: string, isin?: string, issuer: string, quantity?: nu ...

Error in NextJS Middleware Server: Invalid attempt to export a nullable value for "TextDecoderStream"

I've recently created a straightforward Next.js application using bun (version 1.0.4, bun create next-app), incorporating app routing with Next.js version 13.5.4 and a designated source directory. My goal was to implement a middleware that restricts a ...

Regular expressions are effective tools, but they may not be as functional within an

Trying to validate URLs using a regex has been tricky. The regex I have works perfectly fine on regex101.com, but for some reason it keeps failing validation when implemented in my Angular field. I'm at a loss as to how to tweak it so that Angular wil ...

What is the reason behind the mandatory credentials option for the CredentialsProvider?

When using NextAuth.js with a custom sign in page, some code examples for the credentials provider do not include the credentials option in the CredentialsProvider. According to the documentation (here), the credentials option is meant to automatically "ge ...

The language service for Angular is not functioning properly within the VSCode environment

Angular Latest Version Information Package Version ----------------------------------------------------------- @angular-devkit/architect 0.13.6 @angular-devkit/build-angular 0.13.6 @angular-devkit/build-optimizer 0. ...

Modifying the delimiter used in paste range feature of ag-grid

Is there a way to enable pasting tab separated data into an ag-grid column instead of a row? Currently, when pasting newline separated data it goes into columns and tab separated goes into rows. The documentation suggests using the "clipboardDeliminator" ...

When using Angular 4 CanActivate guard in conjunction with a service, the component may not load properly. However, by simply using Observable

When attempting to implement the CanActivate guard in Angular, I encountered an issue where it didn't work when calling a service and using return true, or even return Observable.of(true);. However, the guard did work and the component loaded success ...