eestein
eestein

Reputation: 5114

Angular formgroup not updated with checkbox value

I'm having a hard time figuring out something that is very likely a mistake on my part.

I'm creating a custom dynamic form component that will allow developers to create forms using only TS. The problem is happening with checkboxes. After If I read FormGroup's value before clicking the checkbox it's undefined, if I click on it, it becomes (correctly) true, but if I click again, it remains (uncorrectly) true. What am I missing here?

I've created a stackblitz to show this behavior. Please look at the console for output.

https://stackblitz.com/edit/angular-fydiwl

Thanks!

Upvotes: 2

Views: 934

Answers (1)

yurzui
yurzui

Reputation: 214017

The problem here is that Angular can't recognize the appropriate ControlValueAccessor for your control since your're using dynamic type:

<input ... type="{{inputType}}" 

while accessor for checkbox looks for specific type

@Directive({
  selector: 'input[type=checkbox]...

This means that you have to specify <input type="checkbox" explicitly in order it to be working.

Forked Stackblitz

Upvotes: 5

Related Questions