0

I'm fighting with angular input checkboxes. I have a simple input that looks like this...

<input type="checkbox" ng-model="selectedExercise.isTimed"   required/>

However, when I uncheck the box it removes the .isTimed field from my object, when I want to set it equal to false. For example...

checked

{
  "name": "Work",
  "isTimed": true,
  "duration": 3,
}

unchecked

{
  "name": "Work",
  "duration": 3,
}

I have hacked this to work by adding an ng-change function, but it is super hack in that it just checks to see if angular removed the field, then re-adds it. Is there a proper way to setup my input checkbox so angular won't remove the object field when the box is unchecked?

2
  • this is because of the required. Commented Feb 20, 2015 at 3:04
  • @MamaWalter Thank you! If you post this as an answer I'll send all the karma to you. Commented Feb 20, 2015 at 4:00

2 Answers 2

1

I had same problem. You can solve it just by deleting "required" in checkbox. As I understand it's just

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

2 Comments

It looks like you forgot to finish your answer.
This is correct. MamaWalter answered this in the comments to my OP, but he didn't post it as an answer so I will accept this one.
0

This might have to do with the way that checkboxes are processed. They tend to only return a value when they are true. However, with angular you can override this and capture either state.

<input type="checkbox" ng-model="selectedExercise.isTimed" ng-true-value="yep" ng-false-value="nope"   required/>

1 Comment

I had already tried something similar. Adding... ng-false-value="false" ng-true-value="true" ...has no effect (the field still disappears when I uncheck). Even if I do something like..... ng-false-value="'nope" ng-true-value="'yep'" ... the check value become 'yep' but the field disappears when unchecked.

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.