最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Reactuve Form Control valueChanges get value changed field name , prev value and current value - Stack Over

programmeradmin3浏览0评论
 this.form = this.fb.group({ 
  prop1: '',
  prop2: ''
});

this.form.valueChanges.pipe(startWith(null), pairwise())
  .subscribe(([prev, next]: [any, any]) => {
    console.log('PREV2', prev);
    console.log('NEXT2', next);
  });

using this code I am able to get the previous value and current value. what I want is there any way to get which field is changed, prev value and current value.

like field changed name: prop1, prev value:"", current value: "next value"

 this.form = this.fb.group({ 
  prop1: '',
  prop2: ''
});

this.form.valueChanges.pipe(startWith(null), pairwise())
  .subscribe(([prev, next]: [any, any]) => {
    console.log('PREV2', prev);
    console.log('NEXT2', next);
  });

using this code I am able to get the previous value and current value. what I want is there any way to get which field is changed, prev value and current value.

like field changed name: prop1, prev value:"", current value: "next value"

Share Improve this question edited Feb 1, 2020 at 11:22 Nicholas K 15.4k8 gold badges35 silver badges59 bronze badges asked Feb 1, 2020 at 10:41 Dinesh GanesanDinesh Ganesan 1253 silver badges13 bronze badges 4
  • Do you want to find json difference? – Eranki Commented Feb 1, 2020 at 10:49
  • which form field value is changed that field name, prev value and current value – Dinesh Ganesan Commented Feb 1, 2020 at 11:00
  • There's no build-in RxJS operator but I think lodash has some function for that. – martin Commented Feb 1, 2020 at 11:06
  • I think you should remove startWith operator first and try again but this way will always get you the previous json form and the current json form. – Shorbagy Commented Feb 2, 2020 at 1:23
Add a ment  | 

1 Answer 1

Reset to default 8

Instead of subscribing to theFormGroup.valueChanges observable, you could subscribe to the FormControl.valueChanges observable. See https://angular.io/api/forms/FormControl for the implementation. This way you could have access to the control itself. Maybe something like this?

Object.keys(this.form.controls).forEach(key => {
        this.form.controls[key].valueChanges.pipe(startWith(null), pairwise())
        .subscribe(([prev, next]: [any, any]) => {
            console.log(key) // your FormControl Identifier 
            console.log('PREV2', prev);
            console.log('NEXT2', next);
        });
    });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论