HostListener class

Declares listening to eventName on the host element of the directive.

This annotation is valid on instance methods of a class annotated with either @Directive or @Component, and is inherited when a class implements, extends, or mixes-in a class with this annotation.

@Component(
  selector: 'button-like',
  template: 'CLICK ME',
)
class ButtonLikeComponent {
  @HostListener('click')
  void onClick() {}
}

An optional second argument, args, can define arguments to invoke the method with, including a magic argument '\$event', which is replaced with the value of the event stream. In most cases args can be inferred when bound to a method with a single argument:

@Component(
  selector: 'button-like',
  template: 'CLICK ME',
)
class ButtonLikeComponent {
  @HostListener('click') // == @HostListener('click', const ['\$event'])
  void onClick(MouseEvent e) {}
}
Annotations
  • @Target({TargetKind.method})

Constructors

HostListener(String eventName, [List<String>? args])
const

Properties

args List<String>?
final
eventName String
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited