0

I am trying to show checkboxes checked or unchecked based on certain condition. for eg:

$scope.userRoles = {"grants" : [ 
    "Permission", 
    "View", 
    "Update", 
    "Delete"
]}

On the HTML part i have added the following code:

<div ng-repeat="p in userRoles">
   <input type="checkbox" ng-model="p.grants.indexOf('Delete') != -1?true:false"  ng-change="AddRemovePermission(p,'Delete')" />
</div>

If i use ng-checked instead of ng-model than it works fine, but i will not get 2 way binding with that. Also i know we cant use expressions like above in ng-model. Can anybody help on how this can be done. The only condition is if user has grants than the checkbox should be checked else not, and when clicked on checkbox it should be changed to checked or unchecked accordingly and gets added in the userRoles object. Cant use directive as well.

Thanks.

1
  • for checkbox you need ng-change with ng-model then it will work as two way binding Commented Mar 20, 2018 at 9:38

1 Answer 1

1

The problem is your model. You must send boolean to backend, this can be a solution:

In view:

<div ng-repeat="role in userRoles.grants">
  <input type="checkbox" ng-model="role.checked" />
</div>

And in controller:

$scope.userRoles = {"grants" : [ 
    {"permission": "Permission", checked: true }, 
    {"permission": "View", checked: false }, 
    {"permission": "Update", checked: true }, 
    {"permission": "Delete", checked: true }
]} 
Sign up to request clarification or add additional context in comments.

Comments

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.