bind an event to dom element angular 1

bind an event to dom element angular

import { AfterViewInit, Component, ElementRef} from '@angular/core';

constructor(private elementRef:ElementRef) {}

ngAfterViewInit() {
                                .addEventListener('click', this.onClick.bind(this));

onClick(event) {

Here is what the above code is Doing:
1. We inject the ElementRef into our component.
2. We use the ElementRef to get the native element of our component.
3. We use the native element to query for the my-element element.
4. We add a click event listener to the my-element element.
5. We bind the onClick method to the component instance.
6. We log the event to the console.

Similar Posts