如何在角度2+中将md ElementRef强制转换为HtmlElement

tal*_*tal 8 casting typescript angular

我有

 <input mdInput #try  placeholder="Favorite food" value="Sushi">
Run Code Online (Sandbox Code Playgroud)

而且我得到了它

 @ViewChild('try') myText : ElementRef;
Run Code Online (Sandbox Code Playgroud)

现在我需要得到HtmlElement函数的方法,我怎么能投出它?

并且我不希望通过这种方式添加id="try"到mdInput并通过以下方式 获取:

var cel= document.getElementById("try");
Run Code Online (Sandbox Code Playgroud)

Sar*_*ana 10

myText.nativeElement将包含对DOM元素的引用.断言myText.nativeElementHTMLElement:

let domElement = this.myText.nativeElement as HTMLElement;
Run Code Online (Sandbox Code Playgroud)