Created
April 14, 2021 19:41
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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