What is correct way in Angular (v.5 or v.6) to emit event after specific input of reactive form is changed?
I've already tried this aproaches:
(ngModelChange)="doSomething($event)"
(html, basic event)
(bsValueChange)="doSomething($event)"
(html, ngx-bootstrap specific event)
basic reactive form solution for one input - subscribe for event (ts) like:
this.reactiveForm.get('inputName').valueChanges.subscribe(
value => { this.doSomething(value); }
);
But nothing works in all situations.
Testing:
set input value A
change input value to B
Expected behavior:
reactiveForm.value
items in doSomething()
method have current values.Results:
Approach 1. is not possible to simulate this bug in demo. This is working in my demo but not working in my large project. After I set first date A
and then select date B
, my "doSomething"
which log actual date of field (and value of $event) in reactive form return A
for actual field value. Demo working ok, but I don`t know why I can not achieve such behavior in my project.
Approach 2. not working at all. This is specific event for ngx-bootstrap, but after I select date B
I again get date A
in log.
Approach 3. not working at all. This is pure reactive form solution. You can test it on my demo on the last input. If you type A
and then B
, log from reactiveForm.value['item']
, after you type B
is only A
. So even if you subscribe to valueChanges
event of reactiveForm input, value is not changed.
My question is:
What can I use to catch event for one field of reactive form after this reactive form values are updated?
Here is StackBlitz code DEMO with example
PS: First of all I thought it was a problem of ngx-bootstrap
extension, but it also does not works for pure input of reactive form.
Atiris, the answer is: 3.
But see that is different
this.reactiveForm.value['testDateE'] //or this.reactiveForm.value.testDateE
than
this.reactiveForm.get('testDateE').value;
Is the reason because if you want to check some in the html you must use the second way. When we send a submit, we submit the reactiveForm.value and there are no problem, Equally if you write {{reactiveForm.value |json}} you'll see the actual values