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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With