Declares a host listener.

The decorated method is invoked when the host element emits the specified event.

If the decorated method returns false, then preventDefault is applied on the DOM event.

Example

The following example declares a directive that attaches a click listener to the button and counts clicks.

@Directive(selector: 'button[counting]'')
class CountClicks {
  int numberOfClicks = 0;

  @HostListener('click', const [r'$event.target'])
  void onClick(btn) {
    print("Button $btn, number of clicks: ${numberOfClicks++}.");
  }
}

@Component(
  selector: 'app',
  template: '<button counting>Increment</button>',
  directives: const [CountClicks])
class App {}

Constructors

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

Properties

args → List<String>
final
eventName → String
final
hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(other) → bool
The equality operator. [...]
inherited