1

I am new to Angular JS and working on creating a table where dynamic rows can be added. The new row contains a checkbox which should default to selected (value=Y). For existing data it should show what comes up from DB (Y or N)

<input type="checkbox" ng-model="safetyCheck.needed" 
        value="{{safetyCheck.needed}}" />

I tried adding ng-checked="safetyCheck==Y" and set $safetyCheck:Y when pushing a new blank row. But this always results in Y even if users unselects the component. how can i fix this.

4
  • Read the docs for checkbox. You can't use ng-checked with ng-model Commented Jun 16, 2016 at 0:12
  • thanks , @charlietfl I saw the same. Is there anyother way to do it ? Commented Jun 16, 2016 at 0:20
  • Another way to do what? You clearly didn't read docs thoroughly Commented Jun 16, 2016 at 0:22
  • for new rows i push to the table i have to set default values as selected. I was using ng-checked to set this in the controller I was setting safetycheck.needed as Y for new rows. since i cant use ng-checked how do i set default values Commented Jun 16, 2016 at 0:30

2 Answers 2

2

For specific check\uncheck values use ng-true-value & ng-false-value arguments:

<input type="checkbox"
    ng-model="safetyCheck.needed"
    ng-true-value="'Y'"
    ng-false-value="'N'"/>
Sign up to request clarification or add additional context in comments.

5 Comments

but then how do i set default checked to new rows
@yjoe How You add new rows? I think something like this: $scope.rowsArray.push({needed: 'Y'}); in controller;
yes , thats exactly what I have now. but when i save data even if I unselect the values still goes as Y
Maybe You problem in namespaces? plunker;
Perfect man! I didn't know about this "ng-true-value" and "ng-false-value", this is the right approach. Thank you!
2

ng-checked should not be used together with ng-model. Instead just initialize the variable to true preferably in the controller or wherever you are adding the rows. (Or even in an ng-init as follows)

<input type="checkbox"
ng-model="safetyCheck.needed"
ng-init="safetyCheck.needed=true"/>

1 Comment

For what it's worth. I found this useful when trying to get a check box to default to 0. ng-false-value only seemed to set for me if a user checked and then unchecked. ng-init="my.model=0" worked.

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.