Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: Inject view/DOM into component constructor

I can't figure out how to give a component a reference to its view, to do things like focus on input elements when the form is shown. I can't appear to inject Element or ng.core.ViewRef or ng.core.View into the constructor. How can I get access to the view?

In Angular 1, I would do this with $link.

like image 773
Ben Dilts Avatar asked Jan 02 '16 18:01

Ben Dilts


1 Answers

What you are looking for is probably ElementRef and its nativeElement field, but you should avoid accessing the DOM this way. I think a better way is to add a template variable like <input #inp> and the access it with @ViewChild('inp') input. In ngAfterViewInit input references the input element.

See also angular 2 / typescript : get hold of an element in the template

like image 124
Günter Zöchbauer Avatar answered Sep 20 '22 22:09

Günter Zöchbauer