0
Here is my HTML:
<ul>
   <li ng:repeat="category in categoriesList">
    <a>
      <input type="checkbox" ng-init="isChecked = checkCategory(category)" ng-model='isChecked' checked="isChecked" ><font>
      {{category.name}}</font></a> 
   </li>
   <li>
    <a ng-click='upadteCategoryDetails()'>Apply </a>
   </li>
</ul>

while clicking apply need to update checked categories. If anyone know means please update me. Thanks in advance.

7
  • This will done inside your controller, you should help with the code you have for it so far. One general comment is that it would help to use ng-model in the checkbox like category.checked and a filter in the controller with a function to return checked values. Commented Aug 8, 2014 at 12:59
  • yeah. Am asking sample code for retreiving checked values. Commented Aug 8, 2014 at 13:01
  • what does checkCategory() do? I totally agree with @alou, get rid of ng-init creating a primitive in the repeated child scope Commented Aug 8, 2014 at 13:03
  • checkCategory() --> I used for that category is available or not in my list. That is working fine. I need while clicking apply i need available checked categories. Commented Aug 8, 2014 at 13:07
  • There is no need for apply, if you are using scope variable <-2 way binding-> ng-model, you are in Angular world so get the most of it. Commented Aug 8, 2014 at 13:10

2 Answers 2

1

It would be simpler if you bind the checkbox to a property inside the category object itself, then your Apply can cycle through and gather the categories that are checked and do whatever you want with them.

Here is a working plunker as an example: http://plnkr.co/edit/TabolQ9G3Z3HBm920KaZ?p=preview

 <li ng-repeat="category in categoriesList">
    <input type="checkbox" ng-model="category.isChecked" checked="category.isChecked">
    <font>{{category.name}}</font>
 </li>

And the controller function:

  $scope.upadteCategoryDetails = function(){
    var selectedCategories = '';
    for(var i = 0; i < $scope.categoriesList.length; i++){
      if($scope.categoriesList[i].isChecked)
        selectedCategories += $scope.categoriesList[i].name + ', ';
    }

    $scope.selectedCategories = selectedCategories;
  };

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

ng-model is used inside ng-repeat its better if you have this isChecked within category json itself

 <ul>
   <li ng:repeat="category in categoriesList">
    <a>
      <input type="checkbox" ng-init="category.isChecked=true" ng-model='category.isChecked' checked="category.isChecked" ><font>
      {{category.name}}</font></a> 
   </li>
   <li>
    <a ng-click='upadteCategoryDetails()'>Apply </a>
   </li>
</ul>

Then in updateCategoryDetails you can get categories which are checked with isChecked field.

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.