【发布时间】:2018-06-08 16:37:32
【问题描述】:
我正在尝试从 ts(type script) 文件中获取复选框选中的值。为此,我有一个布尔变量,目的是使用此变量值显示和隐藏 div,但我遇到了问题。请帮助我解决这个问题,并给我正确的方法来做到这一点。这是我的代码...
html代码
**checkbox code**abcde" class="form-check-input" id="abcde" value="1"
(change)="checked('abcde')"> abcde
显示和隐藏代码
*ngIf='shown'
ts 文件
checked(value) {
let get_id = document.getElementById('abcde');
if (get_id.checked == true) {
this.shown = true
}
else if (get_id.checked == false)
this.shown = false;
}
当我运行 ng serve 时,我得到“属性 'checked' 在类型 'HTMLElement' 上不存在”
提前致谢!
【问题讨论】:
-
你使用反应式形式?
-
试试这个
const get_id = document.getElementById('abcde') as HTMLInputElement; -
看看如何在您的问题中插入Code and Reformatted Text。并编辑您的帖子。
-
我使用的是普通形式而不是反应形式..感谢您的回答。使用 const 解决了。谢谢