8

I'm trying to have a box checked by default with a custom value.

Are you ready? <input type="checkbox" ng-model="checked" ng-init="checked=true" ng-true-value="sure" ng-false-value="nope"><br/>
<tt> {{checked}}</tt><br/>

If I set it to that nothing displays until I click the box. Is there a way to where it can be checked with a default value being something other than a boolean value?

1 Answer 1

12

ng-true-value="sure". So, ng-init="checked='sure'"

Code Snippet

<input type="checkbox" ng-model="checked" ng-init="checked='sure'" ng-true-value="sure" ng-false-value="nope">

Plunkr - http://plnkr.co/edit/ejYSsyIjMZSZua9cQyv1?p=preview

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

4 Comments

Wow, I am greatly thankful for your answer. Do you mind explaining why it requires the single quotes? Thanks!
@Sethe23, angularjs will eval the ng-init expression. single quote is javascript syntax to assign value.
I didn't know that it would be evaluate it. I should have tried that, but I couldn't find any documentation to convince me to try so.
@Sethe23, :). Welcome to angularjs. Refer to docs.angularjs.org/api/ng/directive/ngInit . Type for ngInit is expression. If type is expression, angular will eval.

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.