Utilizing stopPropagation in an Angular action menu: A beginner's guide

I'm currently troubleshooting an issue within my accordion setup.

Here's the HTML:

                     <p-menu
                      #menu
                      [popup]="true"
                      [model]="menusMap[item.id]"
                    ></p-menu>
                    <button
                      pButton
                      class="p-button-text"
                      icon="pi pi-ellipsis-h"
                      (click)="menu.toggle($event)"
                    ></button>

In my TypeScript file, I have the following code:

 menusMap: Record<string, MenuItem[]> = {};
  e!: Event;

   function(){
      ......
        if (page.list) {
            const items = page.list;
            this.menusMap = {};
            items .forEach((item) => {
              this.menusMap[item.id] = this.createMenuList(item);
            });
          }
  }

  createMenuList(item: Items): MenuItem[] {
    this.e.stopPropagation(); // Although used, it doesn't prevent accordion display as intended
    return [
      {
        label: '',
        icon: '',
        command: () =>
          this.router.navigate(/aaa)
      }
    ];
  }

The issue arises when clicking the action menu. All I want is for the action menu to open without affecting the accordion. How can I achieve this?

https://i.sstatic.net/oCoyg.png https://i.sstatic.net/UOPDY.png

Answer №1

Here is a helpful tip:

<button
    pButton
    class="p-button-text"
    icon="pi pi-ellipsis-h"
    (click)="$event.stopPropagation(); menu.toggle()"
></button>

Additionally, it is important to note that you are passing the $event parameter to the .toggle() function, but this does not have any effect on your code.

You can create a function in your .ts file and call it from your .html file instead.

For example:

HTML:

<button
    pButton
    class="p-button-text"
    icon="pi pi-ellipsis-h"
    (click)="toggleMenu($event, menu)"
></button>

TS:

public toggleMenu(ev, menuRef) {
    ev.stopPropagation();
    menuRef.toggle();
}

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

Angular - Sweetalrt2 automatically takes action before I even have a chance to press OK

I'm currently utilizing Angular-7 for my website project and have integrated sweetalert2 into it. client.component.ts import { Component, OnInit, ElementRef, NgZone, ViewChild } from '@angular/core'; import { HttpClient } from '@angul ...

Angular: How can I apply animation to rotate an image within a component?

Within my map application, I have implemented a component called compass component. I am seeking a way to programmatically rotate this compass with animation as the map is rotated. The solution involves utilizing angular animation. To achieve this functio ...

Launch a TypeScript Node.js server on Heroku deployment platform

I'm having trouble deploying a Node.js server built with TypeScript on Heroku. Despite following various tutorials and searching through Stack Overflow for solutions, I can't seem to make it work. Here is the code I have in my tsconfig.json and p ...

A step-by-step guide on dynamically altering button colors in Angular 2

Struggling to dynamically change the color of my button, any suggestions? <a class="button buttonaquacss button-mini button-aqua text-right pull-right" (click)='send(button,detail.profile_id)' #button [ngStyle]="{'background-color' ...

The submission of an Angular form results in errors such as being unavailable or

After building a registration page component in Angular and following tutorials, I encountered a frustrating bug. When pressing the submit button on the form, the console would display "undefined" when attempting to access the NgForm's value. However, ...

Is it possible to use null and Infinity interchangeably in JavaScript?

I've declared a default options object with a max set to Infinity: let RANGE_DEFAULT_OPTIONS: any = { min: 0, max: Infinity }; console.log(RANGE_DEFAULT_OPTIONS); // {min: 0, max: null} Surprisingly, when the RANGE_DEFAULT_OPTIONS object is logged, i ...

The issue arises when using NextJs 14 and useContext, as it throws an error despite having the

Struggling with a perplexing issue that I just can't seem to resolve: Here's the scenario: I'm currently working within a "global context" in my NextJs 14 app to store some crucial information that needs to be accessible to different compon ...

Incorporating a binding using [(ngModel)] into a component element

<div *ngFor="let file of files" name="durl" [(ngModel)]="project.durl" > <app-upload-task [file]="file"></app-upload-task> </div> Hello there, I am a beginner in Angular and I have a questi ...

Angular development build fails to start, but the production build successfully runs

I am facing an issue with my Angular 13 project that utilizes Angular Universal (SSR). The problem arises when I try to run the project in development mode using the command ng run project:server --configuration=development. After building the project succ ...

The 'children' property is not found in the type 'ColDef<object>' in <AG-Grid>

Currently working on a project involving AG grid, but encountering an issue with TypeScript. The error message states ([AG-Grid] Property 'children' does not exist on type 'ColDef') and it appears when using the filter method around (co ...

A guide to implementing "Intl.DateTimeFormat" within a Next.js React component

I have a component that is being rendered on the server, making "Intl.DateTimeFormat" accessible. What is the proper way to use "Intl.DateTimeFormat" in a Next.js React component? The error message I am encountering is: Error: There was an error while hy ...

The Angular map function is throwing an error stating "undefined is not a function"

Currently, I'm diving into this fantastic tutorial on Angular 2 and VS Code. Following the steps, I set up a db.json server to experiment with an API using test data structured as follows: { "articles": [{ "id": 1, "name": "Wa ...

Can we define the input and return types for functions using httpsCallables?

I have implemented a callable function in my app to update user claims. The functions are written in Typescript and I have used an interface to define the required data shape for the function. My objective is to make it easier for any developer on the tea ...

Change the default parameter value when optional parameters are present

I am working with a function that has four parameters, where the first parameter is mandatory, the second and third are optional, and the fourth has a default value: class MyClass { static myFunc(param1: string, param2? : string, param3? : string, param ...

Angular - Ensuring the Independence of Field Pairs During Validation

I need to independently validate two sets of fields. One set is for passwords, which should match each other. The second set is for email addresses, which should also match. The current method I have tried isn't working. Can someone guide me on how ...

What could be causing the issue with dayjs dynamic importing in TypeScript?

Currently, I am developing a web screen within a .NET application and facing an issue with sending datetime preferences from the system to the web screen using CefSharp settings. AcceptLanguageList = CultureInfo.CurrentUICulture.Name In my TypeScript code ...

Examined unexplored branch that is a component of the OR condition

I am looking to test an uncovered branch in my codebase. Here is the scenario: https://i.sstatic.net/ZBKgi.png The test involves: describe('addCourseContentCard()', () => { it('should add course content card', () => { ...

Experimenting with a feature that relies on context

I am currently in the process of creating tests for the Login component within my application. The objective is to verify that users can input their email and password, and successfully submit them. However, I am facing two challenges with this task. First ...

Having trouble running Jest tests with three objects in my Vite Vue TypeScript project

Here is a snippet of code that I am testing: import {Line} from "../src/modules/objs/line"; import {SceneWrapper} from "../src/modules/scene/sceneWrapper"; import * as THREE from "three"; import {Dr ...

Creating a dynamic number of properties with different names in Typescript can be achieved by using a loop

I have a requirement for enabling type checking on a specific class Here is the relevant code snippet: class FlightFilter implements Filter { get filters() { return { departTime: { name: 'Departure Time', type: FilterTypes.Range }, ...