Events that do not activate checkboxes in Angular2

I tried everything I could to trigger events when the checkbox changes, but I can't get it to work.

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 the 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 problems I encountered is a button that needs to either be checked or not checked when I receive data from the server. But if they want to deactivate or reactivate the role, I would like her to answer this checkbox.

I tried using [(ngModel)] , (change) and much simpler solutions, but the only thing that triggered any events at all was [(ngModel)] , but in doing so I get true / false instead of 'Y' / 'N' . Also, if I use [(ngModel)] , the flag is always checked, regardless of what data my server sends.

+6
source share
2 answers

You can handle the assignment and change the event handler separately:

 <input type="checkbox" [ngModel]="role?.ACTIVE_FLAG === 'Y' ? true : false" (ngModelChange)="toggleRoleActive()" checked> 
+11
source

I dealt with flags like this

 <input #myId type="checkbox" (change)="myProp = myId.checked" /> 

The component has the myProp property defined by

Here is more details + demo http://www.syntaxsuccess.com/viewarticle/input-controls-in-angular-2.0

+7
source

All Articles