简体   繁体   English

防止复选框 Label 切换复选框

[英]Prevent checkbox Label to Toggle the checkBox

Here is my checkbox in angular and i want to prevent the default behaviour of it.这是我在 angular 中的复选框,我想阻止它的默认行为。

<mat-checkbox (change)="changeSelectionTo()" [(ngModel)]="isChangeRule">Name</mat-checkbox>

Here whenever i click on name label...the checkbox gets toggled which i do not want to.. How can i prevent this from toggling when click on label.在这里,每当我单击名称 label 时...复选框被切换,我不想这样做.. 当单击 label 时,如何防止它切换。 I have tried all others post too but I could not come up with the solution.我也尝试了所有其他帖子,但我无法提出解决方案。 I can not use jQuery .我不能使用 jQuery

在此处输入图像描述 you need to add label after completing the <mat-checkbox> tag.您需要在完成<mat-checkbox>标签后添加 label。

According to your design, you can add style to the <mat-label>根据您的设计,您可以为<mat-label>添加样式

Replace your line of code with the below shown code:用下面显示的代码替换您的代码行:

  <div layout=row>
     <mat-checkbox (change)="changeSelectionTo()" [(ngModel)]="isChangeRule"></mat-checkbox>
    <mat-label> Name </mat-lable>
</div>

above image is generated by below code上图由以下代码生成

<div align="left" class="user-modified-rule">
  <div layout="row">
        <mat-checkbox (change)="changeSelectionTo()" [(ngModel)]="isChangeRule">
        </mat-checkbox>
        <mat-label> 
          <rule></rule>
        </mat-label>
  </div>
</div>

and css code 

.user-modified-rule {
    display: inline-block;
    width : 48%;
}

Based on @Mike S. comment:基于@Mike S. 评论:

<mat-checkbox (change)="changeSelectionTo()" [(ngModel)]="isChangeRule">
  <span (click)="myCheck($event)">Name</span>
</mat-checkbox>

in your component typescript code:在您的组件 typescript 代码中:

myCheck(event) {
    event.preventDefault();
    //YOUR LOGIC
}

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

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