the upward arrow remains steadfast, refusing to evolve into its downward counterpart

Every time I click on "Portfolio", the arrow points downwards.

https://i.sstatic.net/Gwf5F.png

However, when a submenu appears, the arrow changes to point upwards.

https://i.sstatic.net/kFdjr.png

If I click on "Portfolio" again, the arrow remains pointed upwards.

Typically, the arrow should be pointing downwards.

https://i.sstatic.net/dcHmj.png

I am confused about how to address this issue?

admin.component.html

<ul class="nav-links" *ngFor="let menu of menus; let i = index">
<li [ngClass]="{ selected: selectedTab === menu.route }">
   <a
      routerLink="{{ menu.route }}"
      routerLinkActive="active"
      (click)="toggleMenu(i); selectedTab = menu.route"
      >
   <i class="{{ menu.class }}"></i>
   <span class="links_name"> {{ menu.item }} </span>
   <i
      class="{{ menu.arrowDown }}"
      *ngIf="selectedTab !== menu.route"
      style="position: absolute; right: 20px;  "
      ></i>
   <i
      class="{{ menu.arrowUp }}"
      *ngIf="selectedTab === menu.route"
      style="position: absolute; right: 20px;  "
      ></i>
   </a>
</li>

admin.component.ts

export class AdminComponent implements OnInit {
  selectedTab: string;

  showSubmenu: any[] = [];
  showInfo: any[] = [];

  menus: any[] = [
    /* Market */
    {
      class: 'bx bx-grid-alt',
      item: 'Market',
      route: 'market',
      arrowDown: 'bx bx-chevron-down',
      arrowUp: 'bx bx-chevron-up',

      submenus: [
        {
          class: 'bx bx-box',
          item: 'Item',
          route: 'item',
        },
      ],
    },

    /* Currency */

    {
      class: 'bx bx-grid-alt',
      item: 'Currency',
      route: 'currency',
    },

    /* Portfolio */

    {
      class: 'bx bx-box',
      item: 'Portfolio',
      route: 'portfolio',
      arrowDown: 'bx bx-chevron-down',
      arrowUp: 'bx bx-chevron-up',

      submenus: [
        {
          class: 'bx bx-grid-alt',
          item: 'Element',
          route: 'element',
        },
      ],
    },
  ];

  constructor() {}

  ngOnInit() {}

  toggleMenu(index: number) {
    this.showSubmenu[index] = !this.showSubmenu[index];
  }

  toggleSubmenu(event: MouseEvent, item: string) {
    event.stopPropagation();
    this.showInfo[item] = !this.showInfo[item];
  }
}

I have provided the code on Stackblitz for reference.

Thank you for your assistance in clarifying this issue.

Answer №1

When a user clicks on a menu option to expand the submenu, the HTML code assigns the value of menu.route to the variable selectedTab ((click)="toggleMenu(i); selectedTab = menu.route").

If there is a submenu for that clicked option, the code displays the second icon (the arrowUp).

While this is correct, when you click again to collapse the submenu, selectedTab still retains the value of menu.route, causing the second icon (the arrowUp) to remain visible.

To avoid this, you simply need to add a condition to the *ngIf directives that control the visibility of the icons, so that the arrowUp icon is only shown when the submenu is visible (when showSubmenu[i] === true).

For example, you can make this adjustment:

 <i
    class="{{ menu.arrowDown }}"
    *ngIf="selectedTab !== menu.route || !showSubmenu[i]"
    style="position: absolute; right: 20px;  "
 ></i>
 <i
    class="{{ menu.arrowUp }}"
    *ngIf="selectedTab === menu.route && showSubmenu[i]"
    style="position: absolute; right: 20px;  "
 ></i>


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

Tips for incorporating a mail button to share html content within an Angular framework

We are in the process of developing a unique Angular application and have integrated the share-buttons component for users to easily share their referral codes. However, we have encountered an issue with the email button not being able to send HTML content ...

The contents table remains fixed in the top right corner as you scroll

I have developed an Angular app with a table-of-contents component that only displays two items. The code for the script is as follows: ts import { Component, OnInit } from '@angular/core'; import { pdfDefaultOptions } from 'ngx-extended-p ...

Can I modify a global array by updating a dynamically created array in the ngOnInit method of Angular?

Are there any suggestions on how to make a dynamic array available globally in Angular? I am currently using this codepen () which stores clicked countries in an array. The issue is that the array is nested within a function in ngOnInit and I need it to b ...

When working with the "agora-rtc-sdk-ng" package, an error may be thrown by Next.js stating that "window is not defined"

Currently, I am in the process of incorporating the "agora-rtc-sdk-ng" package for live streaming with next.js and typescript. import AgoraRTC from 'agora-rtc-sdk-ng'; However, when I try to import it, an error is thrown as shown below: https:/ ...

Angular 9 Problem: Facing difficulties launching the primary application

Looking for assistance with a technical issue I am trying to set up an initial application on my Windows 10, 64-bit machine following the instructions provided in but I am encountering a problem. OS: Windows 10, 64-bit Issue: The command npm install is ...

The Angular Material autocomplete panel does not disappear when autocomplete is hidden within a scrollable region

https://i.sstatic.net/0eS4M.gif Having encountered a bug, I have raised an issue for it (https://github.com/angular/components/issues/20209). I am reaching out to inquire if there exists any workaround or solution known to anyone. You can observe the pro ...

Angular 2 and beyond: Accessing a child element using @ViewChild()

Is it possible to access the child elements (specifically <img>) of @ViewChild() in Angular 2+ without explicitly declaring them? Within template.html <div #parent> <!-- Other elements besides <img> can also be present --> < ...

Ways to dynamically update the state of a mat-button-toggle-group programmatically

I'm currently working on implementing a confirmation dialog to change the state of MatButtonToggleGroup. However, I am facing an issue where I need to prevent the default behavior unless I revert to the previous state upon cancellation. Here's t ...

The property 'matCellDefTrackBy' cannot be bound to 'mat-cell' as it is not recognized as a valid property

Wondering how to implement trackBy in Angular Material? I encountered the error message below: Can't bind to 'matCellDefTrackBy' since it isn't a known property of 'mat-cell' html: <mat-table [dataSource]="data" ...

Unable to access component properties through react-redux

Context: A React Native application utilizing Redux for managing complexity. Versions: typescript v3.0.3 react-native v0.56.0 redux v4.0.0 @types/react-redux v6.0.9 @types/redux v3.6.0 Issue: The main JSX component in my app is unable to access proper ...

Angular's nested arrays can be transformed into a grid interface with ease

I am looking to generate a grid template from a nested array. The current method works well if the elements naturally have the same height, but issues arise when values are too long and some elements end up with different heights. <div id="containe ...

The program was expecting an array to start, but instead encountered an object. Any suggestions on how to convert

{ "workingHours": [ { "date":"2023-02-01", "amount":3, "freigegeben":false } ] } Whenever I include this in my re ...

Lazy-loading modules in SSR Angular 8 applications are currently unspecified

I am currently in the process of setting up my Angular 8 application to work with server-side rendering (SSR). However, I am encountering some undefined errors in webpack when running my application using ng serve, especially with lazy-loaded modules. Ever ...

When attempting to send a token from an account to a marketplace in ERC721, the transfer caller must either be the owner

Currently, I am in the process of transferring my NFT to a marketplace pragma solidity ^0.8.7; import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import & ...

Identify and handle errors effectively using TypeScript

I have a question regarding my Express server setup. Here is the code snippet: import express from "express"; import helmet from "helmet"; import cors from "cors"; const app = express(); app.use(helmet()); app.use(cors()); a ...

Tips for deleting multiple objects from an array in angular version 13

I am facing an issue where I am trying to delete multiple objects from an array by selecting the checkbox on a table row. However, I am only able to delete one item at a time. How can I resolve this problem and successfully delete multiple selected objects ...

Issues arising with utilizing Github for hosting Angular applications

After developing a site with Angular, everything was running smoothly on my local host. However, when I decided to host the site on GitHub, two errors appeared. You can access my site through this link: Here is a screenshot of the errors encountered [1]: ...

Overriding a generic property in Typescript allows for a more

I'm troubleshooting an issue with my code: class Base<T> {} class Test { prop: Base<any>; createProp<T>() { this.prop = new Base<T>(); } } const test = new Test(); test.createProp<{ a: number }>(); test.pr ...

The property y is not found on type x during property deconstruction

After creating a straightforward projectname.tsx file to contain my interfaces/types, I encountered an issue: export interface Movie { id: number; title: string; posterPath: string; } In another component, I aimed to utilize the Movie interface to s ...

Angular Universal: Missing Title and Meta tags in Page Source

I need help with updating title and meta tags in my Angular Universal Application after a successful api call. app.component.ts import {Component, Inject, OnInit, PLATFORM_ID} from '@angular/core'; import {Meta, Title} from '@angular/platfo ...