简体   繁体   English

如何从angularjs中的js脚本制作单击按钮?

[英]How to make click button from js script in angularjs?

I want to click checkbox from one of my function of script but i do not know how to do it in angularjs? 我想从我的脚本功能之一中单击复选框,但是我不知道如何在angularjs中执行此操作?

I have tried following way but it is not working : 我尝试了以下方法,但无法正常工作:
JS JS

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

HTML 的HTML

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

Can anyone show me how to make checkbox click from js script in angularjs? 谁能告诉我如何从angularjs的js脚本中选中复选框?

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? 如何使复选框从angularjs的js脚本单击?

UPDATED 更新

You can bind scope variables to all checkboxes and change them in the selectAll() method handling the click. 您可以将范围变量绑定到所有复选框,并在处理单击的selectAll()方法中对其进行更改。 Also, you could then bind a method to the selectAllElem checkbox, checking if all other checkboxes are selected. 另外,您还可以将方法绑定到selectAllElem复选框,检查是否选中了所有其他复选框。 I renamed selectAll() to toggleAll() to show how to select/deselect in one method. 我将selectAll()重命名为toggleAll(),以显示如何在一种方法中进行选择/取消选择。

eg: 例如:

<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. 如果复选框是从数组生成的,则可以使用scope.options并在两种方法中对其进行迭代。

OLD

You could bind the input to a scope variable, eg: 您可以将输入绑定到范围变量,例如:

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

And then set that variable from within the function 然后从函数中设置该变量

$scope.allSelected = true; $ scope.allSelected = true;

you can use this instead of above code : 您可以使用它代替上面的代码:

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

in controller create function selectAll(): 在控制器中创建函数selectAll():

$scope.selectAll=function(){
   $scope.allSelected=true;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM