0

I want to listen to the first change by the user in a form, what I need is something like this:

$rootScope.$watch(function() {
            return validationForm.$pristine;
        }, function(newVal, oldVal) {

but in angular 9.

I tried using valueChanges

this.form.valueChanges.subscribe((result) => { })

but it emit on each change in the form build too. how can I subscribe only to the first user's change?

Dvora
  • 23
  • 5

1 Answers1

0

You could use an RxJS pipe like first or take and subscribe to that.

For example:

this.form.valueChanges.pipe(first()).subscribe((result) => { })

or if you wanted to listen to the first 5 changes you could do this:

this.form.valueChanges.pipe(take(5)).subscribe((result) => { })

In case what you're trying to do is validation related you may want to look at status change instead. This answer gives a very short explanation

Adding a minimal stackblitz example of what I understand you are trying to achieve

Aditya Menon
  • 744
  • 5
  • 13
  • but when I did it it emits in the first changes of the form - for each add control to the form too and not only for the user's changes – Dvora Feb 22 '22 at 12:29
  • Assuming that you are manually adding each control you could always place this subscription after you're done adding your controls – Aditya Menon Feb 22 '22 at 13:58
  • it is not a reactive form, I am not adding the controls via type script, I adding it in the html page – Dvora Feb 23 '22 at 06:35
  • I have added a minimal example, might help make things clearer – Aditya Menon Feb 24 '22 at 07:23