Monitoring mouse events on a child element with an attached directive in Angular 7

Is it possible to capture mouse events that occur only within the child element of the directive's host element using @HostListener()?

<div listnerDirective class="parent">
   <div class="child> <------ Listen for mouse events here
   </div>
</div>

Answer №1

Give this a shot:

@HostListener('click', ['$event']) onMouseClick(event: MouseEvent): void {
  // Verify if it's not a child element
  if ( !event.target.classList.contains('parent') ) {
    // The click occurred on the child element
    // Add code for handling child element click here
  }
}

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 term 'Signal' in Angular is typically used as a type, but in this context, it is being treated as a value.ts(2693)

Using angular version 17.2.0, I am facing an issue in my service import { Injectable } from '@angular/core'; import { Signal, ReadOnlySignal } from '@angular/core'; @Injectable({ providedIn: 'root' // Service available thro ...

Managing dependencies and automating setup processes can be tricky when incorporating Typescript into a

Query: How can I easily set up Typescript with Symfony without making extensive changes to Symphony's configuration files? Here are the key requirements for the solution: Typescript MVC Pattern should be set up in a private typescript directory: ...

Here is a method to display a specific string in the mat-datepicker input, while only sending the date in the backend

enter image description hereIn this code snippet, there is a date input field along with a Permanent button. The scenario is that when the Permanent button is clicked, it should display "Permanent" in the input UI (nativeElements value), but the value bein ...

Oops! We encountered an error - the object type '[object Object]' is not supported by NgFor. Remember, NgFor only works with Iterables like Arrays. This issue may be related to nested JSON data

I am attempting to bind the data from a nested Json file provided below. Unfortunately, I am encountering an error that states: "Cannot find a differ supporting object '[object Object]' of type 'object'. ngFor only supports binding to I ...

Exploring the concept of TypeScript interface declaration

I have a function A that will return function B. The parameter of function B is object C, which has a property named D with a type of T. T's value is determined when function B is fetched; this means T can be set when calling function A or using alte ...

Encountering a problem with Angular 11 SSR after compilation, where the production build is causing issues and no content is being displayed in

{ "$schema":"./node_modules/@angular/cli/lib/config/schema.json", "version":1, "newProjectRoot":"projects", "projects":{ "new-asasa":{ "projectType": ...

Is it possible to upgrade just the rxjs version while keeping all other components at their current versions?

While working on my Angular 4 project, I encountered a problem when trying to use a WebSocket package from GitHub. After running npm install to upgrade the rxjs version, I faced errors. Even after attempting to upgrade just the rxjs version and running ng- ...

Tips for displaying the default keyboard with numbers and symbols on Ionic applications

Is there a way to display a keyboard like the image provided by default in an Ionic5 app for Android? I attempted using type="number", but it restricts input to numbers only. However, on iOS, the full keyboard is displayed with this type. When I ...

(TypeScript) Generate a type based on the input parameter type

Below is the code snippet I am working with: const Settings = { setting1: 10, setting2: true, }; type S = typeof Settings; function Process<M extends keyof S>(input: { option: M; value: S[M] }) { } If I try to call Process with an incorr ...

Here's how you can retrieve URL parameters in NextJs, such as `userid/:this_is_a_param`

I'm struggling to retrieve URL parameters using Next.js. I normally do this with Express by getting a :param from the URL like this: users/:userid/ console.log(req.params.userid) All I've been able to do is get the "userid" from the URL like thi ...

Angular 4.x: Service provider missing

I am facing an issue with injecting one service into another service in Angular 4.x, resulting in the error message: Error: No provider for SkillsService! To reproduce this error, I have set up a repository which you can run locally by cloning it and exec ...

Angular form input set to disabled mode

Visit this link for code <form class="example-form"> <mat-form-field class="example-full-width"gt; <mat-label></mat-label> <input matInput placeholder="Ex. Pizza" [disabled]="filterVal ...

managing commitments in TypeScript

Is there a way to convert a promise into a string, or is there another method for handling this result? I am encountering an error stating "You cannot use an argument of type 'Promise' for a parameter of type 'string'." const pokemonIma ...

Retrieve the name of a property or field from an object with a specified type

I have an angular class that is injectable with a readonly property. I do not have control over the initialization of this class as it is meant to be used in a library. Consumers of this library can access these properties but are not allowed to modify the ...

Using AngularFire and Firebase to convert observable arrays

My tech stack includes angular CLI, angularFire, and Firebase. I store my data in a real-time database. https://i.sstatic.net/fEbhN.png I retrieve data from Firebase using an observable: //Service.ts import { Injectable } from '@angular/core' ...

Determining the generic type from supplied Record values in TypeScript: A guide

There is a function called polymorficFactory that creates instances of classes based on a provided 'discriminator' property: type ClassConstructor<T> = { new (...args: any[]): T; }; type ClassMap<T> = Record<string, ClassConstr ...

Could this be a problem within my recursive function?

Struggling to iterate through a stack of HTML Elements, I attempted to write a recursive function with no success. In the code snippet below, I'm facing challenges in returning a value from an if statement and ultimately from the function itself. Wh ...

Is there a way to stop TSC from performing type checking on projects within the node_modules directory

I'm encountering an issue where tsc is performing type-checking on files within the node_modules directory, resulting in errors like: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="513c287c21233e3b34322511617f617f ...

Angular2+ does not return any elements when using .getElementsByClassName() even if they are present

I have a question that seems simple, but I can't seem to find the answer anywhere. I've looked through past questions but still haven't found a solution... In my Angular template, there is a large amount of text inside a div, and some parts ...

The dramatist strategically positioning the cursor at the conclusion of an input field

Currently, I am utilizing playwright for my testing purposes and have encountered a specific issue that I am seeking assistance with. The behavior I need to test is as follows: Applying the bold style to existing text within my input field Verifying that ...