4

I have been searching the threads here but can't really find the answer.

I am trying to get the value for the checked boxes when user check them in Angular. I have something like

<div class="checkbox">

<div class="checkbox" ng-repeat="product in products">
    <input type="checkbox" ng-model="product"/> {{product.name}}
</div>

<button ng-click='add()'></button>

I want to have something in my js like

$scope.add = function() {
    //I want to add the checked product in the selectedProduct array
    $scope.selectedProduct = ['product1', 'product2', 'product4']  => the selected products.
}

How do I do this? Thanks!

3
  • are you specifically asking how to do this with just Angular? or is just a jQuery solution Commented Jul 2, 2014 at 23:19
  • @ScottSelby i need the angular solution. Commented Jul 2, 2014 at 23:21
  • I'd recommend you use this: vitalets.github.io/checklist-model Commented Jul 2, 2014 at 23:30

2 Answers 2

5

The angular solution can be seen here: http://plnkr.co/edit/j63po37JBvc3bFdNAl3T

It's basically sending the event on ng-change

<div class="checkbox" ng-repeat="product in products">
    <input type="checkbox" ng-model="product.selected" ng-change="add(product)"/> {{product.name}}<br>
</div>

and I'm considering your controller to be like this:

app.controller('myController', function($scope){
  $scope.products = [
    {'name':'product1', 'selected': false},
    {'name':'product2', 'selected': false},
    {'name':'product4', 'selected': false}
  ];
  $scope.selected_products = [];

  $scope.add = function(prod){
    var index = $scope.selected_products.indexOf(prod.name);
    if(index == -1 && prod.selected){
      $scope.selected_products.push(prod.name);
    } else if (!prod.selected && index != -1){
      $scope.selected_products.splice(index, 1);
    }
  }
})

So, you have a list of product objects that have a name and the selected state, you use the checkbox to keep the selected state there, and when you mark/unmark it, the ng-change event is triggered, passing to the add function in the scope the product, you then check the product.name index on the selected_products array, if it's not there, you add it, if it is already there, remove it. This way selected_products matches the selected checkboxes.

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

1 Comment

@BenLesh Thanks! IE8 is completely out of my field of vision, thankfully.
2

Use ng-model="product.selected" in your HTML

<div class="checkbox" ng-repeat="product in products">
  <label><input type="checkbox" ng-model="product.selected"/> {{product.name}}</label>
</div>

In your add function, you don't need to keep selectedProducts on your $scope unless you want to display it in your view somewhere, or possibly $watch it for some reason...

I'd recommend just building that array and using it within the closure of your add() function whenever you need it.

JS (for all browsers)

$scope.add = function(){
  var selectedProducts = [];
  angular.forEach($scope.products, function(product) {
    if(product.selected) selectedProducts.push(product);
  });

  // TODO: do something with selectedProducts here
};

JS using Array.prototype.filter (if IE8 isn't an issue)

$scope.add = function(){
  var selectedProducts = $scope.products.filter(function(product) {
    return product.selected;
  });

  // TODO: do something with selectedProducts.
};

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.