Deactivate the click functionality for an element located within a division using Angular and Typescript

Currently, I am working on a web development project using Angular/Typescript.

I am searching for an efficient method to apply the (click) event only to a specific division and not to some selected elements within it.

For instance, let's take this division:

<div class='card' (click)="card_click()">
    <button (click)="whatever($event)">Save</button>
</div>

Clicking the button will trigger both functions.

Typically, one would catch the event in the function and use event.stopPropagation() to stop the propagation.

However, in certain scenarios like a drop-down (as shown in the image below) this approach can get complicated.

https://i.sstatic.net/yYFz9.jpg

In such cases, I utilize the card's event to verify the mouse click target and achieve the desired outcome.

Though not the most efficient solution, it gets the job done.

HTML :

<div class='card' (click)="card_click($event)">
    <button (click)="whatever($event)">Save</button>
</div>

Typescript :

card_click(event) {

    //dropdown button catches SPAN or A
    if(event.target.tagName == "SPAN" || event.target.tagName == "A") {
        event.stopPropagation()
    }
    //proceed with the rest
}

This solution may not be ideal as adding a new element to the card would require editing this function each time.

Are there any other elegant solutions to tackle this issue?

Thank you

Answer №1

To ensure your target is correct, one method is to utilize the contains function, such as

e.target.classList.contains("innerElement")

Next, apply stopPropagation() solely on the click event of "innerElement".

This way, you can designate a class name like "prevent-bubble" where necessary without needing to modify your function each time.

https://stackblitz.com/edit/angular-jh9dpe

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

The Replacement for IonicPage in Ionic 4

I am currently in the process of upgrading my Ionic 3 app to Ionic 4. Does anyone have a solution for converting IonicPage to its Ionic 4 counterpart? I've looked extensively online, but I haven't been able to find comprehensive answers for thi ...

How can I specify the type of a property in Typescript?

Looking for a solution: type WithRequiredProperty<Type, Key extends keyof Type> = Omit<Type, Key> & { [Property in Key]-?: Type[Property]; }; export type MessageWithMdEnforced = WithRequiredProperty<IMessage, 'md'>; ex ...

Differences between making HTTP post requests in Ionic 1 with AngularJS and Ionic 3 with Angular

After working extensively with Ionic 1 AngularJS, I encountered some problems with simple HTTP post requests when transitioning to the new trend of Ionic 3 Angular. Let me provide some context for the scenario. app.module.ts import { HttpModule} from &a ...

I am trying to figure out how to dynamically set the deployUrl during runtime in Angular

When working with Angular, the definition of "webpack_public_path" or "webpack_require.p" for a project can be done in multiple ways: By setting the deployUrl in the .angular-cli.json file By adding --deployUrl "some/path" to the "ng build" command line ...

Navigating the complexities of applying CSS exclusively to child grids within Kendo Angular may seem challenging at first

This image illustrates the grid layout I have created an angular UI using kendo that features a nested grid. I am trying to apply CSS specifically to the child grid without affecting the parent grid. However, no matter what CSS I write, it ends up being a ...

Conditional statements in Typescript can be utilized when dealing with unknown data types

In one of my projects, I have a function that executes an asynchronous operation over a collection. I want to add typings to this function for better IDE support - specifically, I want the IDE to infer that the first parameter in the callback function is o ...

Error Message: "Encountered an Issue with Angular Dynamic

I am currently experimenting with a small reference application to explore the concept of dynamically injecting components into another component in order to display content on a page. Recently, I encountered an error related to the ViewContainerRef object ...

What is the best way to extract data from API console output using Angular?

Currently, I am deeply involved in a full stack project utilizing asp.net and angularjs. My goal is to extract output response from the Swagger API. You can view the code I've written for this purpose here: (https://i.stack.imgur.com/vLyIE.png) The A ...

Limit the allowable React Component prop type in Typescript

To promote composition within our codebase, we utilize passing components as props. Is there a way to enforce type checking for the components passed as props? For instance, let's consider a commonly used Typography component throughout the codebase, ...

Filtering object properties from arrays in Angular HTML can be achieved using various methods such as

I am currently working on a unique "Presentation-Editor" tool where I can see all the presentations that have been created. For this overview, I want to showcase the first slide of each presentation as a preview. However, there is a challenge because I on ...

Encountered a snag during the construction of an Angular 8 SSR application

Currently, I am in the midst of working on an angular 8 project and my goal is to build it for production. However, each time I try running the build command, a critical error arises: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation fai ...

`The date result is displaying inaccurately`

My JSON result appears incorrect vr_date :Date alert(this.vr_date ) // The result displays Thu Feb 07 2019 00:00:00 GMT+0400 var json = JSON.stringify(this.vr_date); alert(json); // The result displays 2019-02-06T20:00:00.000Z, indicating an issue with ...

Creating personalized breakpoints in Material UI using TypeScript

When using the createMuiTheme() function, you have the ability to update breakpoint values like this. const theme = createMuiTheme({ breakpoints: { values: { xs: 0, sm: 600, md: 960, lg: 1280, xl: 1920, }, }, }) ...

Adjustable chart size in React/DC.js based on the size of the window

Trying to adjust the size of my dc.js charts based on window size led me to some helpful examples in the DC.js knowledgebase, with a focus on resizing canvas elements such as this one. However, when I attempt implementation, the chart seems to resize vert ...

Utilize Angular and a store to generate an HTTP request on the local db.json file

Trying to make an HTTP request on a local db.json file has been a challenge for me. (using a store in Angular with the Angular CLI). Unfortunately, my knowledge on this is limited: I have included a snippet of code from my service below: view image descri ...

Retrieve the original HTTP request in Angular before it is sent out

I am currently attempting to retrieve the HTTP requests before they are actually sent to the server. Initially, I tried creating a HttpIntercepter like this: @Injectable() export class HttpLoggingInterceptorProvider implements HttpInterceptor { interce ...

Is there a way to retrieve the component name from the error handler service of an httpInterceptor in Angular?

I'm struggling to retrieve the exact component name, function name, and line number where an error occurred in an Angular TypeScript file from the httpInterceptor. Can anyone assist me in obtaining this information? When I try using console.log(this. ...

Executing Multiple Requests Concurrently in Angular 5 using forkJoin Technique

Important Note The issue lies in the backend, not Angular. The requests are correct. In my Angular5 app, I am trying to upload multiple files at once using rxjs forkJoin. I store the requests in an array as shown in the code below. However, after adding ...

Angular Material Table displaying real-time information

Recently, I've delved into Angular and have been experimenting with creating a dynamic table to showcase data. Currently, I have managed to get it partially working with static data. I drew inspiration from this particular example: https://stackblit ...

I am facing an issue with toastr in Angular 8 as it is not functioning properly and showing me errors

I encountered an issue while trying to use Angular 8 toastr. The error message in the console is as follows: ngx-toastr.js:264 Uncaught TypeError: Object(...) is not a function at ngx-toastr.js:264 at Module../node_modules/ngx-toastr/fesm5/ngx-toastr.js ( ...