简体   繁体   English

“HTMLElement”类型中不存在属性“值”

[英]Property 'value' does not exist on type 'HTMLElement'

Inside a directive in Angular I did the following: Angulardirective中,我执行了以下操作:

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.

相关问题 Textarea 的“HTMLElement”类型上不存在属性“value”-Angular - Property 'value' does not exist on type 'HTMLElement' for Textarea - Angular Typescript 错误“类型‘HTMLElement’上不存在属性‘值’”、“‘元素’类型上不存在属性‘onclick’” - Typescript error "Property 'value' does not exist on type 'HTMLElement'", "Property 'onclick' does not exist on type 'Element'" 类型“JQuery”上不存在属性“slick”<HTMLElement> &#39; - Property 'slick' does not exist on type 'JQuery<HTMLElement>' 类型“ HTMLElement”上不存在属性“ length” - Property 'length' does not exist on type 'HTMLElement' 错误:类型 HTMLElement 上不存在属性“选择” - error:Property 'select' does not exist on type HTMLElement “HTMLElement”类型上不存在属性“r” - Property 'r' does not exist on type 'HTMLElement' 类型“JQuery”上不存在属性“模态”<HTMLElement> &#39; - Property 'modal' does not exist on type 'JQuery<HTMLElement>' 类型“JQuery”上不存在属性“掩码”<HTMLElement> - Property 'mask' does not exist on type 'JQuery<HTMLElement> 类型“ HTMLElement”上不存在属性“ pseudoStyle” - Property 'pseudoStyle' does not exist on type 'HTMLElement' 类型“ HTMLElement”上不存在属性“ width” - Property 'width' does not exist on type 'HTMLElement'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM