简体   繁体   English

使用 angular 材料显示计数复选框

[英]Display count checked checkbox with angular material

I'm trying to display count when checkbox is checked in HTML using Angular Material.我正在尝试使用 Angular 材料在 HTML 中选中复选框时显示计数。

When an user clicks on a checkbox, this one is selected with number 1 instead of a tick or a cross.当用户点击一个复选框时,这个复选框被选中,编号为 1,而不是勾号或叉号。 If the user clicks again, the number 2 is displayed.如果用户再次单击,则显示数字 2。 Do you have an idea how can I achieve this?你知道我怎样才能做到这一点吗? I didn't find any example on Google using Angular material...我在谷歌上没有找到任何使用 Angular 材料的例子......

带有数字单击的复选框

Thank you谢谢

This is really just a number input with a user interface.这实际上只是带有用户界面的数字输入。

I would create a hidden number input that is bound to a property.我将创建一个绑定到属性的隐藏数字输入。 The "checkbox" would display the data-bound property value, and would also increment the value on click. “复选框”将显示数据绑定的属性值,并且还会在单击时增加该值。

<div class="checkbox" [class.checked]="value > 0" (click)="value = value + 1">
  <span *ngIf="value > 0">{{value}}</span>
  <span *ngIf="!value">&nbsp;</span>
</div>
<input type="number" hidden [(ngModel)]="value" />

I would create this as a standalone component with an event emitter so that you can easily place multiple instances in the same component, but the implementation of that is outside the scope of this question.我会将其创建为带有事件发射器的独立组件,以便您可以轻松地将多个实例放置在同一个组件中,但其实现不在此问题的 scope 范围内。

You may also want to read up on the Angular forms ControlValueAccessor so that you can treat your component as a standard Reactive Forms control.您可能还想阅读 Angular forms ControlValueAccessor以便您可以将组件视为标准反应式 Forms 控件。

DEMO: https://stackblitz.com/edit/angular-ufjjkd演示: https://stackblitz.com/edit/angular-ufjjkd

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

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