admin管理员组

文章数量:1289582

Inside a directive in Angular I did the following:

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:

document.getElementById("searchTxt").value;

and document.getElementById() return either null or HTMLElement object:

/** * 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;

so why I get this error ?!!

Inside a directive in Angular I did the following:

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:

document.getElementById("searchTxt").value;

and document.getElementById() return either null or HTMLElement object:

/** * 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;

so why I get this error ?!!

Share Improve this question asked Dec 5, 2018 at 14:04 SlimenTNSlimenTN 3,5648 gold badges37 silver badges84 bronze badges 1
  • Why would you create an input this way ? This kind of issue is usually how you create bad practices : tell us what you are trying to achieve, we'll tell you what you can do to achieve it ! – user4676340 Commented Dec 5, 2018 at 14:09
Add a ment  | 

1 Answer 1

Reset to default 8

Use HTMLInputElement instead:

let input: HTMLInputElement= renderer.createElement('input');

The HTMLElement interface has no "value" property in it, so you get those .ts errors. The HTMLInputElement interface extends HTMLElement and has a "value" property.

本文标签: javascriptProperty 39value39 does not exist on type 39HTMLElement39Stack Overflow