0

I want to click checkbox from one of my function of script but i do not know how to do it in angularjs?

I have tried following way but it is not working :
JS

var a = document.getElementById("selectAllElem");
$scope.selectAll = a["onclick"];
if (typeof($scope.selectAll) == "function") {
    alert("call click");
    $scope.selectAll(a);
}

HTML

<input type="checkbox" ng-click="selectAll($event)" id="selectAllElem" />

Can anyone show me how to make checkbox click from js script in angularjs?

UPDATE
I can not use just single flag variable because above checkbox is placed at header of table and on click of it i need to make all selected check box as deselect and deselected check box as selected. I need to call button click to achieve target.

how to make checkbox click from js script in angularjs?

Sachin
  • 2,321
  • 11
  • 31
  • 49
  • In AngularJS model drives the view behavious. See this SO post http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have-a-jquery-background – Chandermani Jun 09 '14 at 08:08

2 Answers2

0

UPDATED

You can bind scope variables to all checkboxes and change them in the selectAll() method handling the click. Also, you could then bind a method to the selectAllElem checkbox, checking if all other checkboxes are selected. I renamed selectAll() to toggleAll() to show how to select/deselect in one method.

e.g.:

<input ... ng-click="toggleAll()" ng-model="allSelected()" />

and

$scope.allSelected = function(){
  return $scope.option1 && $scope.option2;
}
$scope.toggleAll = function(){
  var value = !$scope.allSelected();
  $scope.option1 = value;
  $scope.option2 = value;
}

In case of the checkboxes being generated from an array, you can have scope.options and iterate over it in both methods.

OLD

You could bind the input to a scope variable, e.g.:

<input type="checkbox" ng-click="selectAll($event)" id="selectAllElem" ng-model="allSelected" />

And then set that variable from within the function

$scope.allSelected = true;

Jasper
  • 1,971
  • 19
  • 34
0

you can use this instead of above code :

<input type="checkbox" ng-click="selectAll()" id="selectAllElem" ng-model="allSelected" />

in controller create function selectAll():

$scope.selectAll=function(){
   $scope.allSelected=true;
 }
Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79