How can I select an element in a component template?
@lebyy▲ 0 net votes
Problem: Does anybody know how to get hold of an element defined in a component template? Polymer makes it really easy with the $ and $$ . I was just wondering how to go about it in Angular. Take the example from the tutorial: import {Component} from '@angular/core';… Fix (from the top answer): Instead of injecting ElementRef and using querySelector or similar from there, a declarative way can be used instead to access elements in the view directly: <input #myname> @ViewChild('myname') input; element ngAfterViewInit() { console.log(this.input.nativeElement.value); } StackBlitz example @ViewChild() supports…