簡體   English   中英

如何在 function 調用 angular 6 時選中 angular 復選框

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

我有一個像這樣的 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>

單擊按鈕時,我需要選中復選框。 我正在使用 angular 6

由於您要綁定到mat-checkbox上的uncheckrx ,因此可以在cancelConfirmDelete方法中簡單地將其設置為true ,這應該可以解決問題。

只需像這樣實現您的功能:

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

這是供您參考的工作樣本StackBlitz

您可以使用ngModel綁定來做到這一點,例如:

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

在組件/容器中將值設置為“ true”,例如在按鈕單擊功能中。 https://material.angular.io/components/checkbox上查看更多

創建 ngModel 綁定以切換檢查狀態

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

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