Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle click events with @Directive?

Tags:

angular

Is it possible to attach click events to the element on or within a @Directive (not @Component)?

For example, I have a @Directive defined as follows:

import { Directive, OnInit, Input, ElementRef, Renderer } from '@angular/core'; declare var $: any; // JQuery  @Directive({     selector: '[gridStack]' }) export class GridStackDirective implements OnInit {     @Input() w: number;     @Input() animate: boolean;      constructor(         private el: ElementRef,         private renderer: Renderer     ) {         renderer.setElementAttribute(el.nativeElement, "class", "grid-stack");     }      ngOnInit() {         let renderer = this.renderer;         let nativeElement = this.el.nativeElement;         let animate: string = this.animate ? "yes" : "no";          renderer.setElementAttribute(nativeElement, "data-gs-width", String(this.w));         if(animate == "yes") {             renderer.setElementAttribute(nativeElement, "data-gs-animate", animate);         }          let options = {             cellHeight: 80,             verticalMargin: 10         };          // TODO: listen to an event here instead of just waiting for the time to expire         setTimeout(function () {             $('.grid-stack').gridstack(options);         }, 300);     }  } 

In the HTML, where I use that @Directive (in index.html), note that I do not have any template associated with the @Directive itself, can I include a click event?:

<div gridStack>     <div><a (click)="clickEvent()">Click Here</a></div> </div> 

I know this works if it is a @Component, but am looking to try to get it working with a @Directive if it is possible.

like image 811
Rolando Avatar asked Jan 31 '17 22:01

Rolando


People also ask

How to pass$ event in Angular?

Events are handled in Angular using the following special syntax. Bind the target event name within parentheses on the left of an equal sign, and event handler method or statement on the right. Above, (click) binds the button click event and onShow() statement calls the onShow() method of a component.

Which directive is used for event binding methods?

Property/event binding with custom attribute directive in Angular.

What is debounce click Angular?

In this post, we will cover the Angular Directive API to create our custom debounce click directive. This directive will handle denouncing multiple click events over a specified amount of time. This directive is used to help prevent duplicate actions.


1 Answers

You can attach to the click event via HostListener decorator in your directive definition. For example:

@Directive({ selector: '[coolBehavior]' }) export class CoolBehaviorDirective {     @HostListener('click', ['$event']) onClick($event){         console.info('clicked: ' + $event);     } } 

See the Angular docs here

And some more info in this question

like image 113
Garth Mason Avatar answered Sep 19 '22 22:09

Garth Mason