1

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.

Nabin Kunwar
  • 1,965
  • 14
  • 29
  • 1
    It's not supposed to fire a change event when the model is updated. You can call `secondEvent()` from `someEvent()` and vice versa to get the same effect. You would need to pass some parameter to prevent an endless loop. – Günter Zöchbauer Jul 17 '17 at 10:22
  • @GünterZöchbauer Is there any way I could do that? From the checkbox? I don't have parameters available in the someEvent. – Nabin Kunwar Jul 17 '17 at 10:27
  • You would just need to pass a value when you call one from the other, not when call it from the event binding. This way you can distinguish if you need to call the other method. If this doesn't help you, please provide more information what problem you're actually trying to solve (instead of getting an event triggered) – Günter Zöchbauer Jul 17 '17 at 10:30
  • @NabinKunwar, why do you need that event? – Max Koretskyi Jul 17 '17 at 10:32
  • I have 3 levels of checkbox and need to pass parent id/ child id depending upon the dom and witch will update other checkbox. – Nabin Kunwar Jul 17 '17 at 10:43
  • @Günter 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 their parentId. – Nabin Kunwar Jul 17 '17 at 11:00
  • You can use an `@Output() myEvent` and then bind from the parent to ` – Günter Zöchbauer Jul 17 '17 at 11:05
  • @GünterZöchbauer Thank you for clearing that out. So now I changed my approach for that. – Nabin Kunwar Jul 18 '17 at 05:21
  • Guys sory but i didnt understand the solution, we have here only one compononent, what child and what parent talking about plz..could you plz explain to me or submit a clear solution using an event thanks – famas23 Dec 27 '18 at 22:52

1 Answers1

0

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")
}