Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onchange equivalent in angular2

Tags:

angular

i'm using onchange to save the value of my input range into firebase , but i have an error who say that my function is not defined.

this is my function

saverange(){   this.Platform.ready().then(() => {     this.rootRef.child("users").child(this.UserID).child('range').set(this.range)   }) }  

this is my html

<ion-item>   <ion-row>     <ion-col>Rayon <span favorite><strong> {{range}} km</strong></span></ion-col>     <ion-col><input type="range" name="points" min="0" max="40" [(ngModel)]="range" onchange="saverange()"></ion-col>   </ion-row> </ion-item> 

what is the equivalent of onchange in angular , if one exist. thank you

like image 312
PrinceZee Avatar asked Apr 01 '16 21:04

PrinceZee


People also ask

How to use onchange in Angular?

The ng-change directive from AngularJS will not override the element's original onchange event, both the ng-change expression and the original onchange event will be executed. The ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus.

When ngOnChanges is called in Angular?

ngOnChanges gets called before ngOnInit and whenever a component's bound input is changed FROM THE PARENT COMPONENT. Remember that ngOnChanges is specific to bound inputs on the component. This means if you don't have any @Input properties on a child, ngOnChanges will never get called.


1 Answers

We can use Angular event bindings to respond to any DOM event. The syntax is simple. We surround the DOM event name in parentheses and assign a quoted template statement to it. -- reference

Since change is on the list of standard DOM events, we can use it:

(change)="saverange()" 

In your particular case, since you're using NgModel, you could break up the two-way binding like this instead:

[ngModel]="range" (ngModelChange)="saverange($event)" 

Then

saverange(newValue) {   this.range = newValue;   this.Platform.ready().then(() => {      this.rootRef.child("users").child(this.UserID).child('range').set(this.range)   }) }  

However, with this approach saverange() is called with every keystroke, so you're probably better off using (change).

like image 121
Mark Rajcok Avatar answered Sep 21 '22 05:09

Mark Rajcok