Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch an event when checking a checkbox in Angular2

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 ?

like image 690
SeleM Avatar asked May 06 '16 16:05

SeleM


People also ask

How do I check if a checkbox is checked in an event?

addEventListener('click', event => { if(event. target. checked) { alert("Checkbox checked!"); } });

Which event is generated when we click check box item?

Whenever a user clicks a Windows Forms CheckBox control, the Click event occurs.

What is the event of checkbox?

The Checkbox Component provided by PrimeNG is an extension of the HTML checkbox with theming and is used to take input of boolean value from the user. There is only one event for the checkbox component, that is, when the value of the checkbox changes, the onChange event is fired.

How do you checked the checkbox in angular?

AngularJS ng-checked DirectiveThe ng-checked directive sets the checked attribute of a checkbox or a radiobutton. The checkbox, or radiobutton, will be checked if the expression inside the ng-checked attribute returns true. The ng-checked directive is necessary to be able to shift the value between true and false .


2 Answers

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   } } 
like image 131
Ankit Singh Avatar answered Sep 20 '22 19:09

Ankit Singh


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()" /> 
like image 21
Jakob Lithner Avatar answered Sep 22 '22 19:09

Jakob Lithner