函数调用angular 6时如何选中angular复选框

时间:2019-01-25 06:09:40

标签: html angular typescript

我有一个类似HTML的复选框和函数

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

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

单击按钮时,我需要选中该复选框。我正在使用角度6

2 个答案:

答案 0 :(得分:1)

由于您要绑定到垫子复选框上的uncheckrx,因此可以简单地在true方法中将其设置为cancelConfirmDelete,这应该可以解决问题。

只需实现如下功能:

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;
  }
}

  

这是您推荐的Working Sample StackBlitz

答案 1 :(得分:0)

您可以使用ngModel绑定来做到这一点,例如:

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

在组件/容器中将值设置为“ true”,例如在按钮单击功能中。 在https://material.angular.io/components/checkbox

中查看更多信息