0

I want to check/uncheck checkbox based on the value of the field services.Register.IsTest.

When services.Register.IsTest=True My checkbox should be checked else not

my checkbox

<input type="checkbox" id="patReturned" value="Returned">

I am new to AngularJS. Please help.

Thanks

1
  • <input type="checkbox" id="patReturned" value="Returned" checked="services.Register.IsTest"> will work if services.Register.IsTest is a scope variable. Commented Aug 5, 2016 at 13:02

3 Answers 3

2

Your are looking for the ngChecked directive from AngularJS

Sets the checked attribute on the element, if the expression inside ngChecked is truthy.

Use it like this

<input type="checkbox" id="patReturned" value="Returned" ng-checked="services.Register.IsTest">
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

<input type="checkbox" ng-checked="services.Register.IsTest == true" ng-model="services.Register.IsTest" ng-true-value="true" ng-false-value="false">

Comments

0

Just add ng-model directive if you need two-way binding (i.e. services.Register.IsTest will be updated when the checkbox is checked or unchecked):

<input type="checkbox" id="patReturned" value="Returned" ng-model="services.Register.IsTest">

Make sure services.Register.IsTest is a part of the relevant scope.

Besides that, you can use ng-checked directive to avoid changing services.Register.IsTest:

<input type="checkbox" id="patReturned" value="Returned" ng-checked="services.Register.IsTest">

2 Comments

Not going to down vote this but this is not what was asked as this would change the value of servces.register.IsTest if the checkbox is un-checked
@GeorgeLee If you're talking regarding ng-model, yeah, you're right, thanks. I've added a few words to explain this aspect. But the second part of my answer (ng-checked) is what we have been asked for.

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.