Angular 5触发自定义函数的最佳ng-change / ngChange事件是什么?

时间:2018-04-11 14:16:21

标签: angular input angular5 angularjs-ng-change

我试图让ng-change事件更新我写的这个函数。目前,该功能仅将“我在这里”记录到控制台。我这样实现它:

<input type="number" ng-minlength="1" ng-maxlength="3" min="5" max="100" class="form-control" name="percentOwnership" (ng-change)="fullyOwned()"  [(ngModel)]="individualowner.percentOwnership">

我试图取消单向绑定parens并添加双向绑定parens和括号,但没有任何东西激活我的功能来记录“我在这里”到控制台。

我也通过使用“change”和“ngModelChange”尝试了这些组合。尽管如此,没有任何东西可以解雇我我正在使用Angular 5.2.9

1 个答案:

答案 0 :(得分:2)

您需要分别使用[ngModel](ngModelChange)

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <input type="number" [ngModel]="percent" (ngModelChange)="onPercentChange($event)" />
  `,
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  percent = 20;

  onPercentChange(percent: number) {
    console.log('here');  

    this.percent = percent;
  }
}

Live demo

此外,您的min="5" max="100"验证将无法按预期运行。它们已在Angular 4.2中删除。 Read more about the issue