Search code examples
javascriptangulartypescriptdevextremedevextreme-angular

Uncheck always checkbox


I am using a checkbox and I hope it is never checked.

I used a function for this, but it is not working, if you check the checkbox a few times it is active (checked).

How can I keep it always unchecked?

DEMO

.TS

check: boolean = false;

change(e){
  this.check = false
}

.HTML

<ul class="mdc-image-list my-image-list" style="margin-top:120px;padding-left: 10px;padding-right: 10px;">
    <ng-container *ngFor="let product of data; let  j = index;">
        <li class="mdc-image-list__item">
            <div class="mdc-image-list__image-aspect-container">
                        <img [src]="product.image" class="mdc-image-list__image">
            </div>
            <div class="mdc-image-list--with-text-protection">
                <div class="mdc-image-list__supporting mdc-image-list__supporting">
                    <span class="mdc-image-list__label">{{product.name}}</span>
                </div>
                <div class="Info">
                    <dx-check-box (onValueChanged)="change($event);" [(value)]="check"></dx-check-box>
                </div>
            </div>
        </li>
    </ng-container>
</ul>

Problem

enter image description here


Solution

  • You can use [ngModel] binding to display the check value and (ngModelChange) event handling to process the change:

    <dx-check-box [ngModel]="check" (ngModelChange)="change($event)"></dx-check-box>
    

    In the event handler:

    1. Set the new check value
    2. Force change detection by calling ChangeDetectorRef.detectChanges
    3. Set the check value back to false
    change(value) {
      this.check = value;
      this.changeDetectorRef.detectChanges();
      this.check = false;
      ...
    }
    

    See this stackblitz for a demo.