Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 select and unselect a checkbox

Alright I have a terms of service modal which is an ngBootstrap modal and when I press the button to close that button I want the action that closes the modal define wheter the checkbox is checked or not This is the html:

  <ng-template #content let-c="close" let-d="dismiss">
  <div class="modal-header">
  <h4 class="modal-title">Terms of service.</h4>
  <button type="button" class="close" aria-label="Close" (click)="d('Cross 
  click')">
  <span aria-hidden="true">&times;</span>
  </button>
  </div>
  <button type="button" class="btn btn-success" (click)="c('Close click'); 
   setAccepted(true)" >I accept.</button>
  </ng-template>

the link to open the modal and the checkbox

<div class="custom-control custom-checkbox">
            <input *ngIf="accepted" type="checkbox" class="custom-
control-input" id="save-info" required>
            <label class="custom-control-label" for="save-info">I have read 
</label><a href="javascript:void(0)" (click)="open(content)"> 
  the terms of service</a>.
</div>

And under it I have <p>{{accepted}}</p> just for testing

And the typescript

accepted: boolean = false;

constructor(private modalService: NgbModal) {}

open(content) {
 this.modalService.open(content);
}

setAccepted(accepted:boolean){
  this.accepted = accepted;
}

I tried [(ngModel)], *ngIf, ngModel to the accepted boolean from my typescript but nothing seems to work.

like image 724
Burst of Ice Avatar asked Jan 01 '26 13:01

Burst of Ice


1 Answers

Use "[checked]" input propery or attribute. Use a Boolean to check on uncheck the checkbox.

In Template:

    <input [checked]="accepted" type="checkbox" class="custom-
control-input" id="save-info" required>

In TS:

accepted: Boolean;
accepted = true;   // Or False
like image 172
Basavaraj Bhusani Avatar answered Jan 03 '26 11:01

Basavaraj Bhusani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!