0

I have a password input component, which is a custom form control.

The problem is, when I type into the input, the form value (created with FormGroup) is not being updated.

However, when I use form.get('key').setValue('value here'), the form value as well as the component are updated correctly.

What might be the issue?

Using Angular v13.2.1 • Whole source codecomponent with the formpassword input component
Wanted to upload it to Stackblitz, but was getting many unrelated errors.

3
  • 1
    the links are dead... can you paste the code here? Commented Aug 7, 2022 at 13:21
  • Hi there joker876! While I'm sure this question was in a good place nearly two years ago, you've got dead links for your original problem code, which renders the question less helpful than it was. While pasting code into a text editor can be onerous, I recommend trying to recover your code and paste it into this question so that people can learn from the problem you solved. Commented Sep 21, 2023 at 16:08
  • The links are now working. Commented Jun 27, 2024 at 8:09

1 Answer 1

2

writeValue function is called by angular when the input in the ui gets updated. onChange should be called by the component whenever an internal change should be reflected in the ui. So it's not quite right to call onChange whenever writeValue gets called by angular. Instead, you should set the form control value in onChange. Also, by putting [value] = "value" on the input, and calling onInput(), you're calling on change with an outdated value of the value variable. Try using 2 way binding using [(ngModel)] = "value" and (ngModelChange) instead.

Sign up to request clarification or add additional context in comments.

2 Comments

the ngModel and ngModelChange combo made it work. Thanks!
But, what should be the case with reactive angular forms? @jamal-salman

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.