简体   繁体   English

如何使用 Angular8 检测输入的变化

[英]How to detect change in input using Angular8

i have edit and close button in same row, if there is any edit made to the input field then it must show an alert message, if no change is made it must retain its old value.我在同一行有编辑和关闭按钮,如果对输入字段进行了任何编辑,那么它必须显示一条警报消息,如果没有进行更改,它必须保留其旧值。 My issue here is, i click on edit and do some change to input, when i click on close, i am able to retain new value, but when i click on close, it must revert to old value我的问题是,我点击编辑并对输入进行一些更改,当我点击关闭时,我可以保留新值,但是当我点击关闭时,它必须恢复为旧值

TS: TS:

 public onEditEvent(event) {
    this.editCommitment = event;
  }

  public onCloseEvent(event){
    if(event.policyCT == this.editCommitment.policyCT && event.quotes == this.editCommitment.quotes && event.writtenPremium == this.editCommitment.writtenPremium) {
      event.writtenPremium = this.editCommitment.writtenPremium;
      event.policyCT = this.editCommitment.policyCT;
      event.quotes = this.editCommitment.quotes 
      this.editableRow = 0;
    } else {
      alert('change')
    }
  }

Demo 演示

By default ng-model will update the model.默认情况下 ng-model 将更新模型。 You need to maintain old value manually when user clicks on edit button.当用户单击编辑按钮时,您需要手动维护旧值。 Like below:-如下图:-

 selectedRow: any;
  public onEditEvent(event) {
    this.selectedRow = { ...event };
    this.editCommitment = event;
  }

Apply that when user click on close button.当用户单击关闭按钮时应用它。

  public onCloseEvent(event) {
    event.writtenPremium = this.selectedRow.writtenPremium;
    event.policyCT = this.selectedRow.policyCT;
    event.quotes = this.selectedRow.quotes;
    this.editableRow = 0;
  }

And on Save click you dont have to do anything as model is already updated.在保存单击时,您无需执行任何操作,因为模型已经更新。

Working Demo:- https://stackblitz.com/edit/angular-turxyo?file=src%2Fapp%2Fapp.component.ts工作演示:- https://stackblitz.com/edit/angular-turxyo?file=src%2Fapp%2Fapp.component.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 angular8 和 bootstrap4 检测保存和标签更改事件 - How to detect save and tab change event using angular8 with bootstrap4 使用angular8取消选中复选框时如何保持禁用输入 - How to keep input disabled when the checkbox is unchecked using angular8 如何使用angular8限制输入字段中的保留字符 - How to restrict reserved characters in input field using angular8 如何使用angular8切换行 - how to toggle the row using angular8 如何基于使用 angular8 打字使输入显示为句子大小写 - How to make input appear as sentence case based on typing using angular8 如果没有使用 Angular8 在输入字段中进行编辑,如何禁用保存按钮 - How to disable Save button, if there is no edit made in input field using Angular8 如何使用angular8根据上一个和下一个按钮单击使表单中的数据发生变化 - How to make the data in form to change based on previous and next button click using angular8 如果我们有更多记录而没有使用 angular8 中断,如何更改下拉值 - How to change the dropdown value, if we have more records without having break using angular8 如何防止用户在 Angular8 中连续输入“00”? - How do I prevent user Input continuous "00" in Angular8? 如何使用反应形式使用angular8比较对象数组 - How to compare array of objects using angular8 using reactive forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM