Javascript – how to uncheck/check a primeng checkbox manually

angularcheckboxhtmljavascriptprimeng

html

<p-checkbox name="showLinkedRisksOnly" id="showLinkedRisksOnlyChkBx" 
   label="Show Only Linked Risks" binary="true" (click)="showOnlyLinkedRisks($event)"
   [ngModel]="showLinkedRisksOnly" ></p-checkbox>

typescript

showOnlyLinkedRisks($event){
  if(condition){
    this.showLinkedRisksOnly = !this.showLinkedRisksOnly;
  }
}

I am trying to change the state of checkbox back to before it was checked/unchecked based on condition. But for some reason the checkbox and model get out of sync when I do change the value of this.showLinkedRisksOnly.
Is it possible to achive

Best Answer

First, binding onChange event instead of click event. Then add checkbox instance to event

<p-checkbox name="showLinkedRisksOnly" #something id="showLinkedRisksOnlyChkBx" label="Show Only Linked Risks" binary="true" (click)="showOnlyLinkedRisks($event, something)" [ngModel]="showLinkedRisksOnly"></p-checkbox>

And in typescript

showOnlyLinkedRisks(event, control) {
    if (false) {
      control.checked = false;
    } else { this.showLinkedRisksOnly = event.checked; }
}

Sorry for my bad english!