0

I am new to AngularJS and facing one problem with multiple check-boxes. I have one registration form in which I have choices of colors which comes from database.

$scope.ColorList = { { ID: 1,Name:"Red" },{ ID: 2,Name:"Green" },{ ID: 3,Name:"Blue" }};

I am using below code to render checkbox in form.

<tr>
  <td>Favorite Colors</td>
  <td>
     <label data-ng-repeat="c in item.ColorList">
        <input type="checkbox" value="{{c.ID}}" /><span>{{c.Name}}</span> 
     </label>
  </td>
</tr>

Now, during add operation, checkboxes renders properly... But how to bind checkboxes with model so that I get an array of selected checkboxes?

Also during edit time, I need to pre-select checkboxes to display user's saved choices.

So how to achieve it?

Thanks in advance.

2
  • @DharemeshSolanki is that a typo shouldn't it be [ { ID:1, Name:"Red"} ...] Commented Apr 25, 2014 at 11:59
  • You can use this solution: stackoverflow.com/questions/20968170/… Commented Apr 25, 2014 at 12:04

1 Answer 1

0

I think most people would have used c as their model and then tied ng-model to some property on c. Then your list is really just item.ColorList

$scope.ColorList = [ { ID: 1,Name:"Red" },{ ID: 2,Name:"Green" },{ ID: 3,Name:"Blue" }];

<label data-ng-repeat="c in item.ColorList">
    <input type="checkbox" ng-model="c.Active" value="{{c.ID}}" /><span>{{c.Name}}</span> 
 </label>

$scope.getSelected = function(item){
     var results = [];
     angular.forEach(item.ColorList, function(c){
          if(c.Active){
              results.push(c);
          }
     });
     return results;
}
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.