I am trying to filter only changed values over FormGroup
when I subscribe other functions to it.
So with example, my formGroup is like
this.myForm = new FormGroup({
name: new FormControl(''),
address: new FormGroup({
street: new FormControl(''),
postcode: new FormControl('8000'),
country: new FormControl(''),
state: new FormControl(''),
})
});
I have nested form with multiple fields. Now I wish to subscribe various events, over different fields. Like street has something different, a state has a different function, the country other. So I am calling subscribe on ngInit
. It gives me the issue when it loads it calls multiple times so I used debounceTime
to control that and make it executes after 2 mins. But then also it executes for 1 time. and gives me empty value. so I could use skip(1)
in that case. But if I call multiple functions over multiple fields of address, I need to write separate subscribe over each field that would execute multiple times again.
Q1. Could I club it to single function so my valueChanges filters only changed values?
Suppose in one go I changed the city, so data
in subscribe should return only address.city
. I would write single function then, depending on conditions. But every time it returns whole formValues. I am expecting to filter only changed values in the form.
this.myForm.valueChanges
.debounceTime(2000)
.subscribe((data) => {
console.log(data);
});
Q2. Does writing .takeUntil(this.unsbs)
would automatically unsubscribe it like this
this.unsbs(){
this.myForm.next();
this.myForm.complete();
}
or I need to unsubscribe to other Subscription type variable?