Skip to content

Instantly share code, notes, and snippets.

@semagarcia
Created April 14, 2021 19:41
Show Gist options
  • Save semagarcia/223336c451f940c63ca565264fdbde12 to your computer and use it in GitHub Desktop.
Save semagarcia/223336c451f940c63ca565264fdbde12 to your computer and use it in GitHub Desktop.
OPTION B: "Angular way" => Class (in Angular example, use this snippet inside AfterViewInit lifecycle hook)
// OPTION B: "Angular way" => Class
// In Angular example, use this snippet inside AfterViewInit lifecycle hook:
class MyAngularComponent implements AfterViewInit {
// Through @ViewChild decorator, Angular will get the first element matches selector in the DOM
@ViewChild('counter', { static: false }) counter;
ngAfterViewInit() {
// Angular-way
this.counter.nativeElement
.addEventListener('counter-clicked', this.myCallbackFunction);
}
myCallbackFunction() { ... }
}
/*
In order to select the element into de DOM, we need to add the selector.
HTML will be like:
<click-counter #counter></click-counter>
Where "@counter" is the selector used by @ViewChild decorator
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment