2

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!

2
  • follow the steps in this article. this is the correct way to build custom form items. alligator.io/angular/custom-form-control. in short you implement ControlValueAccessor and then can that component as you would a native input element. Commented Jun 28, 2019 at 18:02
  • @delashum thanks, I was able to fix the code using yurzui's answer. But I'll definitely take a look at the article later. Commented Jun 28, 2019 at 18:30

1 Answer 1

5

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

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

2 Comments

Thanks, that was it. Guess I should've checked the source code first :D. thanks for your help :)
I think that it's not such transparently so it is not your fault

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.