Angular How to trigger change event of checkbox after model value change?

2.7k Views Asked by At

I want to trigger change event of checkbox two on checking on checkbox one. So far after second one is checked when first one is checked. But I couldn't get second one to fire its change event Here's my code This is my template code

<input [ngModel]="firstOne" (ngModelChange)="someEvent($event)" type="checkbox">
<input [ngModel]="secondOne" (ngModelChange)="secondEvent($event)" type="checkbox">

Controller code

this.firstOne:boolean=false;
this.secondOne:boolean=false;

constructor(){}
someEvent(event){
  this.secondOne = true;
}

secondEvent(event){
 alert("second is triggered")
}

Thanks for any suggestion. I have tree of check box with their itemId and parentId. On initialization the api i am using provides last child's itemId. So my idea is to check the last child check box using the itemId and then trigger events which will check its parents using its parentId causing a chain check.

1

There are 1 best solutions below

0
On

If you need update checked status use

[checked]="secondOne"

You cannot call event from another checkbox. You can only call method from another methods

constructor(){}
someEvent(event){
  this.secondOne = true;
  this.secondEvent();
}

secondEvent(event?){
 alert("second is triggered")
}