对于Textarea - Angular,类型'HTMLElement'上不存在属性'value' [英] Property 'value' does not exist on type 'HTMLElement' for Textarea - Angular

查看:302
本文介绍了对于Textarea - Angular,类型'HTMLElement'上不存在属性'value'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Angular 4(typescript),我有以下HTML代码:

Using Angular 4 (typescript), I have the below HTML code:

 <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>

我正在尝试使用以下内容将用户输入的数据输入textarea:

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'

这应该是一个简单的函数。我究竟做错了什么? W3schools从textarea获取值显示'.value'作为必需的函数!

This should be a simple function. What am I doing wrong? W3schools "getting values from textarea" shows '.value' as the required function!

推荐答案

你只需断言该类型您的元素是 HTMLTextAreaElement 。因为 document.getElementById 返回 HTMLElement 而且并非所有html元素都具有 property:

You just have to assert that the type of your element is HTMLTextAreaElement. Because document.getElementById returns HTMLElement and not all html elements have the value property:

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

但是当您使用Angular时,您可能应该使用数据绑定而不是自己查询值

But seeing as you use Angular, you should probably be using data binding instead of querying the values yourself

这篇关于对于Textarea - Angular,类型'HTMLElement'上不存在属性'value'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆