Launch an event when checking a checkbox in Angular2

118,096

Solution 1

StackBlitz

Template: You can either use the native change event or NgModel directive's ngModelChange.

<input type="checkbox" (change)="onNativeChange($event)"/>

or

<input type="checkbox" ngModel (ngModelChange)="onNgModelChange($event)"/>

TS:

onNativeChange(e) { // here e is a native event
  if(e.target.checked){
    // do something here
  }
}

onNgModelChange(e) { // here e is a boolean, true if checked, otherwise false
  if(e){
    // do something here
  }
}

Solution 2

If you add double paranthesis to the ngModel reference you get a two-way binding to your model property. That property can then be read and used in the event handler. In my view that is the most clean approach.

<input type="checkbox" [(ngModel)]="myModel.property" (ngModelChange)="processChange()" />

Solution 3

You can use ngModel like

<input type="checkbox" [ngModel]="checkboxValue" (ngModelChange)="addProp($event)" data-md-icheck/>

To update the checkbox state by updating the property checkboxValue in your code and when the checkbox is changed by the user addProp() is called.

Solution 4

Check Demo: https://stackblitz.com/edit/angular-6-checkbox?embed=1&file=src/app/app.component.html

  CheckBox: use change event to call the function and pass the event.

<label class="container">    
   <input type="checkbox" [(ngModel)]="theCheckbox"  data-md-icheck 
    (change)="toggleVisibility($event)"/>
      Checkbox is <span *ngIf="marked">checked</span><span 
     *ngIf="!marked">unchecked</span>
     <span class="checkmark"></span>
</label>
 <div>And <b>ngModel</b> also works, it's value is <b>{{theCheckbox}}</b></div>
Share:
118,096
SeleM
Author by

SeleM

Experienced Full Stack Developer with a demonstrated history of working in the computer software industry. Mainly skilled in Javascript &amp; Java. First Angular badged Tunisian on Stack Overflow and the -118th (bronze) | 101st (silver)- globally. Certified in various computer science fields. Challenges are my cup of tea! Want to check your rank among answerers from your country in diverse technologies ? I'd created this for you : Top users per technology by country , Enjoy !

Updated on June 12, 2021

Comments

  • SeleM
    SeleM almost 3 years

    I'm newbie in Angular2 and in web globally , I want to launch an action that changes an oject paramater value in the Database when checking a checkbox and or unchecking it using Material-Design, I tried with [(ngModel)] but nothing happened. the idea is that i have to add some propositions with checked | unchecked status to tell if it is a true or false proposition. Here is the proposition model

    export class PropositionModel {
        id:string;
        wordingP:string; // the proposition
        propStatus:Boolean; // the proposition status
    }
    

    here is the Html code for a proposition :

    <div class="uk-width-xlarge-1-1 uk-width-medium-1-2">
                    <div (submit)="addProp1()" class="uk-input-group">
                        <span class="uk-input-group-addon"><input type="checkbox"  data-md-icheck/></span>
                        <label>Proposition 1</label>
                        <input [(ngModel)]="proposition1.wordingP" type="text" class="md-input" required class="md-input"/>
                    </div>
                </div>
    

    here is the TypeScript code for adding the proposition:

    addProp1() {
            this.proposition1 = new PropositionModel();
            this.proposition1.propStatus = false;
            this.propositionService.addProposition(this.proposition1)
                .subscribe(response=> {
                    console.log(response);
                    console.log(this.proposition1);
                    this.proposition1 = new PropositionModel();})
        }
    

    And as you can see i made it a false by default for the proposition status and I want to change it once i checked the proposition. Here is an image how it looks for a better issue understanding. enter image description here

    Any help Please ?

  • SeleM
    SeleM almost 8 years
    didnt work, sorry , can you clarify more according to the code above ? i'll appreciate it ..
  • Günter Zöchbauer
    Günter Zöchbauer almost 8 years
    [ngModel]="checkboxValue" adds the ngModel directrives and default ControlValueAccessor (and binds the value of checkboxValue to the checkbox). With ngModel we can bind to the ngModelChange event that is emitted every time the checkbox changes.
  • Günter Zöchbauer
    Günter Zöchbauer almost 8 years
    What Angular"'2 version are you using. There were issues with binding to some input elements in FF and IE but they were fixed recently.
  • SeleM
    SeleM almost 8 years
    it is Beta 0, and i didn't try to update it, The problem is that the ngModel is not considering my changes ..
  • Günter Zöchbauer
    Günter Zöchbauer almost 8 years
    You would need at least beta.16 AFAIR
  • Sacky San
    Sacky San over 7 years
    Note that it's (change) and not (onChange). It was my silly mistake that took my a few minutes to figure out