[英]Replacing document.getElementById('checkBox') in Angular
I am trying to replace the document.getElementById
from:我正在尝试从以下位置替换
document.getElementById
:
target = document.getElementById('checkBox') as HTMLInputElement;
I have tried:我努力了:
and this does not seem to be working.这似乎不起作用。
you don't have to use template reference (#target) or ViewChild to change the value of the checkbox input.您不必使用模板引用 (#target) 或 ViewChild 来更改复选框输入的值。
you have two options:你有两个选择:
1- Two-way Data Binding 1-双向数据绑定
create a property inside your component and bind it to the input(checkbox) using [(ngModel)]="property_name"
now you can change the value of this property and the checkbox will be changed在您的组件中创建一个属性并使用
[(ngModel)]="property_name"
将其绑定到输入(复选框)现在您可以更改此属性的值并且复选框将被更改
2- Reactive form 2-反应形式
if you are using this input inside a reactive form, just add a FormControl to the FormGroup you are using and use this FormControl to change the value如果您在反应式表单中使用此输入,只需将 FormControl 添加到您正在使用的 FormGroup 并使用此 FormControl 更改值
this.form_group_name.get('form_control_name').setValue(true / false);
this.form_group_name.get('form_control_name').patchValue(true / false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.