javascript - 类型 'value' 上不存在属性 'HTMLElement'

标签 javascript angular

Angulardirective 中,我做了以下操作:

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/

相关文章:

javascript - ECMA6Script 中 [].forEach.call(...) 的替代方案

javascript - D3 不显示 SVG 文本元素

Angular 2 : directive causes HTTP response to take long when scrolling with mousewheel

angular - Asp.net Core 和 Angular 7 Cors

具有 ControlValueAccessor 测试的 Angular 2(4) 组件

html - 从表中的每 2n 个元素中删除边框间距

javascript - 用户登录前的弹窗检测

javascript - IE9 上的 JQUERY 水平菜单

javascript - MVC - 基于 HTML 属性添加到模型

javascript - d3.js v5.9.1 - 此版本中的某些方法未按预期工作