Skip to content

Instantly share code, notes, and snippets.

@matthieu-D
Last active April 2, 2017 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieu-D/c56ce33e844b1f6e6d692149c31bb83b to your computer and use it in GitHub Desktop.
Save matthieu-D/c56ce33e844b1f6e6d692149c31bb83b to your computer and use it in GitHub Desktop.
import {ElementRef, Renderer, ViewChild, Component} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor (private renderer: Renderer){ }
@ViewChild('ulEl') ulEl: ElementRef;
ngOnInit() {
this.tmpListener = this.renderer.listen(this.ulEl.nativeElement, 'click', this.logElement);
}
// Shorthand to get event.target
logElement({target}) {
if(target && target.nodeName == "LI") {
console.log('Target id: ', target.id);
// Add Business Logic here
}
}
ngOnDestroy() {
this.tmpListener();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment