Ben Dilts Ben Dilts - 3 months ago 30
AngularJS Question

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.

Answer

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.