Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events not firing within checkboxes in Angular2

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.

like image 904
Morgan G Avatar asked Feb 07 '16 20:02

Morgan G


People also ask

How to define checkbox check event in angular?

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.

How to define event call function in angular?

<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 ()”.

How to define radio button event in angular?

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.

What are the types of events handling in angular?

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.


2 Answers

You can handle assignment and change event handler separately:

<input type="checkbox" 
  [ngModel]="role?.ACTIVE_FLAG === 'Y' ? true : false"        
  (ngModelChange)="toggleRoleActive()" checked>
like image 173
Günter Zöchbauer Avatar answered Oct 05 '22 10:10

Günter Zöchbauer


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

like image 31
TGH Avatar answered Oct 05 '22 11:10

TGH