简体   繁体   English

如何在 function 调用 angular 6 时选中 angular 复选框

[英]How to make angular checkbox checked when a function call angular 6

I have an HTML like this for checkbox and my function我有一个像这样的 HTML 复选框和我的 function

<mat-checkbox 
  [checked]="uncheckrx" 
  (change)="onClickExpandRx($event.checked)">
  Rx
</mat-checkbox>

<button 
  class="btn btn-default" 
  id="cancelConfirmForDelete" 
  (click)="cancelConfirmDelete()" 
  translate>
  Cancel
</button>

When clicking the button I need to make the checkbox checked.单击按钮时,我需要选中复选框。 I am using angular 6我正在使用 angular 6

Since you're binding to uncheckrx on your mat-checkbox, you can simply set that to true inside the cancelConfirmDelete method and that should do the trick. 由于您要绑定到mat-checkbox上的uncheckrx ,因此可以在cancelConfirmDelete方法中简单地将其设置为true ,这应该可以解决问题。

Just implement your functions like this: 只需像这样实现您的功能:

import {Component} from '@angular/core';

@Component({
  selector: 'checkbox-overview-example',
  templateUrl: 'checkbox-overview-example.html',
  styleUrls: ['checkbox-overview-example.css'],
})
export class CheckboxOverviewExample {
  uncheckrx;

  onClickExpandRx(checked) {
    this.uncheckrx = checked;
  }

  cancelConfirmDelete() {
    this.uncheckrx = true;
  }
}

Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

You can do it with a ngModel binding like: 您可以使用ngModel绑定来做到这一点,例如:

<mat-checkbox
    [(ngModel)]="value"
    name="test">THIS IS A CHECKBOX</mat-checkbox>

Set in your component/container the value to "true", for example in your button click function. 在组件/容器中将值设置为“ true”,例如在按钮单击功能中。 See more at https://material.angular.io/components/checkbox https://material.angular.io/components/checkbox上查看更多

Create ngModel binding to switch checked status创建 ngModel 绑定以切换检查状态

<mat-checkbox  [(ngModel)]="isOtherChecked">
      Other (please specify):
    </mat-checkbox>

TS TS

export class AppComponent {

  isChecked:boolean = false;

dropdownChangeEvent(event: any) {    
  this.isChecked = true;
}

} }

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

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