7

I have an issue with my AngularJS checkbox where they become undefined when they are unchecked. Can't figure out why this is happening.

<input type="checkbox" name="available" ng-model="formData.available" ng-true-value="1" ng-false-value="0" required>

With the above code; default on page load is 0. when Its checked it becomes 1. When I uncheck it it becomes undefined

Any help appreciated.

6
  • 2
    there's an issue with the 'required' attribute Commented Jan 31, 2016 at 12:39
  • Can you explain more please Commented Jan 31, 2016 at 12:39
  • Is this only one option or are there others? Can't have required if the user has the option to check or uncheck with one option. Commented Jan 31, 2016 at 12:41
  • Of course, this is how it should be since you have required validation rule. Commented Jan 31, 2016 at 12:46
  • Thanks guys removed required and working as expected Commented Jan 31, 2016 at 12:48

2 Answers 2

5

Thanks to the comments I've realised that the required option is not necessary and was the cause of the issue. Thanks guys!

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

Comments

5

If you need the required attribute set ng-model-options="{ allowInvalid: true }" so undefined becomes false on the model.

https://docs.angularjs.org/api/ng/directive/ngModelOptions

Comments

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.