我不知道如何为组件提供对其 View 的引用,以便在显示表单时执行诸如关注输入元素之类的操作。我似乎无法将 Element
或 ng.core.ViewRef
或 ng.core.View
注入(inject)构造函数。我怎样才能访问 View ?
在 Angular 1 中,我会使用 $link 来完成此操作。
最佳答案
你要找的大概是ElementRef
及其 nativeElement
字段,但您应该避免以这种方式访问 DOM。
我认为更好的方法是添加一个模板变量,如 <input #inp>
并使用 @ViewChild('inp') input
访问它.在 ngAfterViewInit
input
引用输入元素。
另见 angular 2 / typescript : get hold of an element in the template
关于angularjs - Angular 2 : Inject view/DOM into component constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569387/