与angular2中的onchange等效 [英] onchange equivalent in angular2

查看:58
本文介绍了与angular2中的onchange等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用onchange将输入范围的值保存到firebase中,但是我有一个错误,谁未定义我的函数.

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.

这是我的职责

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

这是我的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>

如果存在onchange,则等于angular的onchange. 谢谢

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

推荐答案

我们可以使用角度事件绑定来响应任何DOM事件. 语法很简单.我们将DOM事件名称括在括号中,并为其添加一个带引号的模板语句. -参考

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

由于change位于标准DOM事件列表上,因此我们可以使用它:

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

(change)="saverange()"


在您的特定情况下,由于您使用的是NgModel,因此您可以这样拆分双向绑定:


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)"

然后

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

但是,通过这种方法,每次击键都会调用saverange(),因此最好使用(change).

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

这篇关于与angular2中的onchange等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆