[英]Property 'value' does not exist on type 'HTMLElement'
Inside a directive
in Angular
I did the following: 在
Angular
的directive
中,我执行了以下操作:
let input: HTMLElement = renderer.createElement('input');
renderer.appendChild(this.elem.nativeElement, input);
and when I try to get the value writen inside this input using: 当我尝试使用以下方法获取此输入中的值时:
console.log('value', input.value);
I get this error: 我收到此错误:
Property 'value' does not exist on type 'HTMLElement'.
Although we all now that to get value from input using javascript we do the following: 虽然我们现在都是使用javascript从输入中获取价值,但我们执行以下操作:
document.getElementById("searchTxt").value;
and document.getElementById()
return either null or HTMLElement
object: 和
document.getElementById()
返回null或HTMLElement
对象:
/** * Returns a reference to the first object with the specified value of the ID or NAME attribute.
/ ** *返回对具有指定ID或NAME属性值的第一个对象的引用。 * @param elementId String that specifies the ID value.
* @param elementId指定ID值的字符串。 Case-insensitive.
不区分大小写。 */ getElementById(elementId: string): HTMLElement |
* / getElementById(elementId:string):HTMLElement | null;
空值;
so why I get this error ?!! 为什么我会收到这个错误?!!
Use HTMLInputElement
instead: 改为使用
HTMLInputElement
:
let input: HTMLInputElement= renderer.createElement('input');
The HTMLElement interface has no "value" property in it, so you get those .ts errors. HTMLElement接口中没有“value”属性,因此您会收到这些.ts错误。 The HTMLInputElement interface extends HTMLElement and has a "value" property.
HTMLInputElement接口扩展了HTMLElement并具有“value”属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.