angular - angular2 中的 onchange 等价物

标签 angular

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

这是我的职责

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>

angular 中的 onchange 是什么,如果存在的话。 谢谢

最佳答案

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

因为 changethe list of standard DOM events 上,我们可以使用它:

(change)="saverange()"

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

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

关于angular - angular2 中的 onchange 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366375/

相关文章:

Angular APP_INITIALIZER 在测试中加载配置文件

Angular 6 : get reference to Components created with *ngFor inside ng-container tag

angular - 我如何在 Angular 7 中使用 Chrome 中的 PasswordCredential API

javascript - TypeScript - "debugger;"语句在 Angular 中使用 QA 环境时不起作用

json - GroupByWithSumPipe 对列表进行排序并对分组项的属性求和 Angular

angular - 如何使用最新的 Rxjs 版本实现 http post 超时?

Angular:将两个表单控件绑定(bind)到同一个模型

angular - 如何在包含 Angular 4 应用程序的 Docker 容器中使用 'reach' Neo4j Bolt?

Angular 2 与 ngClass 的双向绑定(bind)

angular - AOT - Angular 6 - 指令 SomeComponent,预期 0 个参数,但得到 1 个。对于自制组件