Siddharath Gupta Siddharath Gupta - 3 months ago 12
AngularJS Question

Condition on checked property of checkbox in html

How can I put condition on checked property of checkbox?

I have checkboxes for each row in my application. In my database, I have a column 'IsSaved' whose value is either 0 or 1. If IsSaved for a row is 1 then checkbox should automatically be shown as selected.

Here's my code snippet:

<td style="text-align:center;">
<input type="checkbox" name="select" value="${ID}" ng-checked="${IsSaved} ? true : false" />

How can I achieve this?


You can access variables of your scope directly by calling it by its variable name: IsSaved.

You are using a ternary expression, this however is not supported as described here.

Now you can use a shorter expression for this: IsSaved == 1, which just results in a truthy or falsy result.

So you get

<input type="checkbox" name="select" id="{{ID}}" ng-checked="IsSaved == 1" />  

Note: that ID variable can be accessed in the scope by the double braces.