I have tried everything I can think of to get events to fire when a checkbox changes state but I can't seem to get it working.
Here is the html
<div *ngIf="role?.ACTIVE_FLAG === 'Y'">
<div class="label"> Active Record </div>
<input type="checkbox" (click)="toggleRoleActive()" checked>
</div>
<div *ngIf="role?.ACTIVE_FLAG === 'N'">
<div class="label"> Active Record </div>
<input type="checkbox" (click)="toggleRoleActive()">
</div>
Here is toggleRoleActive()
Edit:
toggleRoleActive(){
if(this.role_submit.ACTIVE_FLAG === 'Y'){
this.role_submit.ACTIVE_FLAG = 'N';
}else {
this.role_submit.ACTIVE_FLAG = 'Y';
}
}
One of the challenges I am facing, is the button needs to either be checked or not checked when I get data from the server. But if they want to deactivate or reactivate the role I would like it to respond to the checkbox.
I have tried using [(ngModel)]
, (change)
and much simpler solutions, but the only thing that has triggered any events at all is [(ngModel)]
, but doing it that way I end up with true
/false
instead of 'Y'
/'N'
. Also if I use [(ngModel)]
, the checkbox is always marked checked regardless of what data my server sends.
When user checks or unchecks checkbox or when user clicks on radio button then this event fires. For defining this event in Angular we need to define it as (change)=”yourfunction ()”. For using forms in our app we need to import FormModule in our module file.
<input (select)="eventCallFunction ('select Event')" value=""/> This event fires on radio button or checkbox state change. When user checks or unchecks checkbox or when user clicks on radio button then this event fires. For defining this event in Angular we need to define it as (change)=”yourfunction ()”.
For defining this event in Angular we need to define it as (select)=”yourfunction ()”. This event fires on radio button or checkbox state change. When user checks or unchecks checkbox or when user clicks on radio button then this event fires.
In this article we will see events handling in Angular. In this article we will cover all types of events like input events , button events, form events, mouse events, checkbox events , radio button events etc. I hope you will find this article helpful.
You can handle assignment and change event handler separately:
<input type="checkbox"
[ngModel]="role?.ACTIVE_FLAG === 'Y' ? true : false"
(ngModelChange)="toggleRoleActive()" checked>
I have been dealing with checkboxes like this
<input #myId type="checkbox" (change)="myProp = myId.checked" />
The component has a myProp
property defined
Here is more info + a demo http://www.syntaxsuccess.com/viewarticle/input-controls-in-angular-2.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With