angular - 通过用户在 angular2 中的输入触发方法的最佳实践

标签 angular event-handling user-input

我有这种情况:期待用户的输入并将其用于来自服务器的_http.get()。 我试过使用 keyup 事件,但无法管理。非常感谢任何建议。

我的模板:

<div class="col-md-8">
   <input type="text" class="form-control" [(ngModel)]="workOrder" (keyup)="populate()" placeholder="{{ 'FIELDS.PMS_IFR_WO' | translate }}"/>
</div>

我的 ts 文件:

populate(){
  console.log(this.workOrder);
  this._lookup.getIcrData().subscribe(res => {
    console.log(res);
  });
}

最佳答案

您正在寻找 ngModelChange:

<div class="col-md-8">
    <input type="text" class="form-control" [ngModel]="workOrder" (ngModelChange)="populate($event)" placeholder="{{ 'FIELDS.PMS_IFR_WO' | translate }}"/>
</div>

组件

populate(value: any){
  console.log(value);
  this._lookup.getIcrData().subscribe(res => {
        console.log(res);
  });
}

关于angular - 通过用户在 angular2 中的输入触发方法的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47631667/

相关文章:

java - 将 GWT 事件挂接到外部 iframe 中的元素

c - 将值设置为用户定义大小的二维数组的元素

javascript - Angular 2,如何编写自定义 mousedown/touchstart 实现

javascript - 无法使用 Angular 选择 ngoni 上的所有检查

ionic-framework - 行为主体和本地存储

javascript - 使用 Jquery 允许常规 JS 对象上的事件

Angular2 变化检测 : ngOnChanges not firing for nested object

java - 当我清楚地用 Java 将它写在它所指的类中时,错误显示方法未定义?

c++ - 使用cstring数组c++循环获取用户输入

PHP 清理粘贴的 Microsoft 输入