admin管理员组文章数量:1291202
I am using Angular 4 and on my template I have a checkbox and a div.
In my .ts file I have 2 functions.
// html
<input type="checkbox" class="custom-control-input" (change)="function2($event)">
<div (click)="function1()">some text here</div>
This I have the ts file
// .ts
function1() {
// check if the checkbox is checked.
}
function2(event) {
// do something here
}
From function1 how can I check if the checkbox is checked or not?
I am using Angular 4 and on my template I have a checkbox and a div.
In my .ts file I have 2 functions.
// html
<input type="checkbox" class="custom-control-input" (change)="function2($event)">
<div (click)="function1()">some text here</div>
This I have the ts file
// .ts
function1() {
// check if the checkbox is checked.
}
function2(event) {
// do something here
}
From function1 how can I check if the checkbox is checked or not?
Share Improve this question asked Aug 19, 2017 at 20:51 user8398743user8398743 02 Answers
Reset to default 8One of the ways to get the value in function1() is to use template variable.
Then you can do the followings:
1. HTML
<input #input type="checkbox" class="custom-control-input" (change)="function2($event)">
Typescript
@ViewChild('input') private checkInput;
....
function1(){
console.log(this.checkInput.checked? "it's checked": "it's not checked")
}
2. HTML
<input #input type="checkbox" class="custom-control-input" (change)="function2($event)">
<div (click)="function1(input)">some text here</div>
Typescript
function1(element){
console.log(element.checked? "it's checked": "it's not checked")
}
Add an Id attribute to the checkbox
<input id='check1' type="checkbox" class="custom-control-input" (change)="function2($event)">
Then check the value in function1()
function1() {
if(document.getElementById('check1').checked) {
// do something here
}
本文标签: javascriptAngular check if checkbox is check from inside a functionmethodStack Overflow
版权声明:本文标题:javascript - Angular check if checkbox is check from inside a functionmethod - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503347a2382176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论