onchange equivalent in angular2
Solution 1:
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)
.
Solution 2:
In Angular you can define event listeners
like in the example below:
<!-- Here you can call public methods from parental component -->
<input (change)="method_name()">