Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox input undefined when unchecked - AngularJS

Tags:

angularjs

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.

like image 333
moh_abk Avatar asked Jan 31 '16 12:01

moh_abk


2 Answers

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

like image 148
moh_abk Avatar answered Sep 23 '22 07:09

moh_abk


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

like image 23
Dustin Page Avatar answered Sep 24 '22 07:09

Dustin Page