Javascript 在(更改)事件回调上具有旧值的角度反应形式

Javascript 在(更改)事件回调上具有旧值的角度反应形式,javascript,angular,angular2-forms,angular-reactive-forms,angular-event-emitter,Javascript,Angular,Angular2 Forms,Angular Reactive Forms,Angular Event Emitter,考虑一个有输入的角度反应形式。每当输入发生变化时,我们都希望保留其旧值,并将其显示在某个位置。以下代码按显示方式执行此操作: @组件({ 选择器:“我的应用程序”, templateUrl:“./app.component.html”, 样式URL:['./app.component.css'] }) 导出类AppComponent{ 名称='反应形式'; 改变价值; 旧价值; ooldValue; rform=新表单组({ inputOne:newformcontrol('change me

考虑一个有输入的角度反应形式。每当输入发生变化时,我们都希望保留其旧值,并将其显示在某个位置。以下代码按显示方式执行此操作:

@组件({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
名称='反应形式';
改变价值;
旧价值;
ooldValue;
rform=新表单组({
inputOne:newformcontrol('change me')
});
onOneChange(事件){
this.changedValue=event.target.value;
console.log('oneChanged',this.changedValue,'old value is',this.oldValue);
this.ooldValue=this.oldValue;
setTimeout(()=>this.oldValue=this.changedValue,1);
}
}

一:

更改的值:{{changedValue}}

旧值:{{ooldValue}}

valueChanges是可观察的,因此您可以成对地通过管道获取订阅中的上一个和下一个值

// No initial value. Will emit only after second character entered
this.form.get('inputOne')
  .valueChanges
  .pipe(pairwise())
  .subscribe(([prev, next]: [any, any]) => ... );
// Fill buffer with initial value, and it will emit immediately on value change
this.form.get('inputOne')
  .valueChanges
  .pipe(startWith(null), pairwise())
  .subscribe(([prev, next]: [any, any]) => ... );

valueChanges是可观察的,因此您可以成对地通过管道获取订阅中的上一个和下一个值

// No initial value. Will emit only after second character entered
this.form.get('inputOne')
  .valueChanges
  .pipe(pairwise())
  .subscribe(([prev, next]: [any, any]) => ... );
// Fill buffer with initial value, and it will emit immediately on value change
this.form.get('inputOne')
  .valueChanges
  .pipe(startWith(null), pairwise())
  .subscribe(([prev, next]: [any, any]) => ... );

考虑添加一些关于这是如何工作的解释。是的,但是我认为JunLAN的答案更健壮。我从来没有听说过两两,所以我也学到了一些新东西。考虑添加一些关于这是如何工作的解释。是的,但是我认为JunLAN的答案更健壮。我从来没有听说过两两,所以我也学到了一些新东西。