简体   繁体   English

Angular 7,没有NgModel时如何取复选框状态(是/否)?

[英]Angular 7, without NgModel how to take the checkbox status (true / false)?

In my Angular 7 application, I have the collection of the checkbox which comes inside the *ngFor without [(ngModel)]. 在我的Angular 7应用程序中,我具有* ngFor中没有[(ngModel)]的复选框的集合。 Here, How can I take the checkbox status whether it is checked or not without using ngmodel. 在这里,如何在不使用ngmodel的情况下获取是否处于选中状态的复选框状态。

I have tried $event, but it comes always true. 我已经尝试过$ event,但它总是正确的。 Find the below my code and give any good solution without [(ngModel)]. 找到下面的代码,并给出没有[[ngModel)]的任何好的解决方案。

<div *ngFor="let country of addedValue  | keyvalue;  let in = index; ">
          <div>
              <input type="checkbox" name="country{{in}}" (change)="selectTestAll(country.value, $event)">
            </div>
</div>

 selectTestAll(countryValue: any, event: any) {
    console.log(event); // HERE I NEED TO KNOW THAT CHECKBOX IS CHECKED OR NOT
 }

You can use the checked property. 您可以使用选中的属性。 Accessing it via event.target . 通过event.target访问它。 This is example is using object destructuring assignment to retrieve the checked from event.target : 这是使用对象解构分配event.target中检索已checked 对象的示例:

selectTestAll(countryValue: any, event: any) {
  const { checked } = event.target;
  console.log(checked);
}

Here is an example in action. 这是一个实际的例子

Hopefully that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM