简体   繁体   English

Textarea 的“HTMLElement”类型上不存在属性“value”-Angular

[英]Property 'value' does not exist on type 'HTMLElement' for Textarea - Angular

Using Angular 4 (typescript), I have the below HTML code:使用 Angular 4(打字稿),我有以下 HTML 代码:

 <div *ngIf="dataService.selected_markers.length == 0" id="tsnPasteContainer">
      <form style="width: 100%; height: 100%">
        <textarea id="tsn_list" style="width: 100%; height: 100%" type="text" name="tsn_list" placeholder="e.g. 2001311,2425302,2153542,2435974"></textarea>
      </form>
    </div>

I am trying to get the data that the user is typing into the textarea using:我正在尝试使用以下方法获取用户在文本区域中输入的数据:

public parseTSNs(){
    let tsnString = document.getElementById("tsn_list").value;
    console.log("User inputted string: " + tsnString);
}

This function is called by a button.该函数由按钮调用。

The code is not compiling due to:由于以下原因,代码未编译:

Property 'value' does not exist on type 'HTMLElement'

This should be a simple function.这应该是一个简单的功能。 What am I doing wrong?我究竟做错了什么? W3schools "getting values from textarea" shows '.value' as the required function! W3schools“从文本区域获取值”将“.value”显示为所需函数!

You just have to assert that the type of your element is HTMLTextAreaElement .您只需断言元素的类型是HTMLTextAreaElement Because document.getElementById returns HTMLElement and not all html elements have the value property:因为document.getElementById返回HTMLElement并且并非所有 html 元素都具有value属性:

let tsnString = (document.getElementById("tsn_list") as HTMLTextAreaElement).value;

But seeing as you use Angular, you should probably be using data binding instead of querying the values yourself但是当你使用 Angular 时,你可能应该使用数据绑定而不是自己查询值

I had faced a similar issue while setting the value of a cell in the table.在表格中设置单元格的值时,我遇到了类似的问题。

I was adding the data as follows:我按如下方式添加数据:

document.getElementById("table_id").rows[0].cells[0].innerHTML = "data_to_be_added";

And I was getting the following error:我收到以下错误:

error TS2339: Property 'rows' does not exist on type 'HTMLElement'错误 TS2339:属性“行”在类型“HTMLElement”上不存在

Property 'cells' does not exist on type 'HTMLElement'类型“HTMLElement”上不存在属性“单元格”

So I added the following tag and the ERROR got resolved.所以我添加了以下标签,错误得到解决。

(document.getElementById("table_id") as any).rows[0].cells[0].innerHTML = "data_to_be_added";

Here, you are expicitly changing the type of the variable as any.在这里,您明确地将变量的类型更改为任何类型。

You should use: [(ngModel)]='yourValue' to bind data to your controller.您应该使用: [(ngModel)]='yourValue'将数据绑定到您的控制器。 Textarea doesn't use value parameters. Textarea 不使用value参数。

Pleas read more information about HTML tag and its attributes hire .阅读有关 HTML 标记及其属性的更多信息。

Although it's not supposed to work like that, If you want to do it like this, first give your tag a reference with a # :虽然它不应该像那样工作,但如果你想这样做,首先给你的标签一个带有#的引用:

<div *ngIf="dataService.selected_markers.length == 0" id="tsnPasteContainer">
    <form style="width: 100%; height: 100%">
        <textarea #myTA style="width: 100%; height: 100%" type="text" name="tsn_list" placeholder="e.g. 2001311,2425302,2153542,2435974"></textarea>
    </form>
</div>

Then, in your component, import this:然后,在你的组件中,导入这个:

import {ViewChild, ElementRef } from '@angular/core';

Declare a variable matching your reference:声明一个与您的参考相匹配的变量:

@ViewChild('myTA') myTextArea: ElementRef;

In your method:在你的方法中:

parseTSNs() {
    let el = this.myTextArea.nativeElement as HTMLElement;
    console.log(el.value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM