在 Angular
的 directive
中,我做了以下操作:
let input: HTMLElement = renderer.createElement('input');
renderer.appendChild(this.elem.nativeElement, input);
当我尝试使用以下方法获取此输入中写入的值时:
console.log('value', input.value);
我收到这个错误:
Property 'value' does not exist on type 'HTMLElement'.
虽然我们现在都知道要使用 javascript 从输入中获取值,但我们会执行以下操作:
document.getElementById("searchTxt").value;
和 document.getElementById()
返回 null 或 HTMLElement
对象:
/** * Returns a reference to the first object with the specified value of the ID or NAME attribute. * @param elementId String that specifies the ID value. Case-insensitive. */ getElementById(elementId: string): HTMLElement | null;
那么为什么我会收到此错误?!!
最佳答案
改用 HTMLInputElement
:
let input: HTMLInputElement= renderer.createElement('input');
HTMLElement 接口(interface)中没有“值”属性,因此您会遇到那些 .ts 错误。 HTMLInputElement 接口(interface)扩展了 HTMLElement 并具有“值”属性。
关于javascript - 类型 'value' 上不存在属性 'HTMLElement',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53634133/