角度检查是否从函数/方法内部检查复选框

max*_*020 1 javascript typescript angular2-template angular

我正在使用Angular 4,在我的模板上我有一个复选框和一个div.

在我的.ts文件中,我有2个函数.

// html
<input type="checkbox" class="custom-control-input" (change)="function2($event)">

<div (click)="function1()">some text here</div>
Run Code Online (Sandbox Code Playgroud)

这我有ts文件

// .ts

function1() {
   // check if the checkbox is checked.      
}

function2(event) {
    // do something here
}
Run Code Online (Sandbox Code Playgroud)

从function1我如何检查是否选中了复选框?

Veg*_*ega 5

在function1()中获取值的方法之一是使用模板变量.

然后你可以做到以下几点:

1. HTML

<input #input type="checkbox" class="custom-control-input" (change)="function2($event)">
Run Code Online (Sandbox Code Playgroud)

打字稿

@ViewChild('input') private checkInput;
....
function1(){
  console.log(this.checkInput.checked? "it's checked": "it's not checked")
}
Run Code Online (Sandbox Code Playgroud)

2. HTML

<input #input type="checkbox" class="custom-control-input" (change)="function2($event)">
<div (click)="function1(input)">some text here</div>
Run Code Online (Sandbox Code Playgroud)

打字稿

function1(element){
      console.log(element.checked? "it's checked": "it's not checked")
 }
Run Code Online (Sandbox Code Playgroud)