【问题标题】:How to make click button from js script in angularjs?如何从angularjs中的js脚本制作点击按钮?
【发布时间】:2014-06-10 18:44:50
【问题描述】:

我想从我的脚本功能之一中单击复选框,但我不知道如何在 angularjs 中执行此操作?

我尝试了以下方法,但它不起作用:
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" />

谁能告诉我如何从 angularjs 中的 js 脚本中单击复选框?

更新
我不能只使用单个标志变量,因为上面的复选框被放置在表格的标题中,点击它我需要将所有选中的复选框设为取消选中并将取消选中的复选框设为选中。 我需要调用按钮点击来实现目标。

如何从 angularjs 中的 js 脚本中单击复选框?

【问题讨论】:

标签: javascript angularjs


【解决方案1】:

你可以用这个代替上面的代码:

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

在控制器中创建函数selectAll():

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

【讨论】:

    【解决方案2】:

    更新

    您可以将范围变量绑定到所有复选框,并在处理单击的 selectAll() 方法中更改它们。 此外,您可以将一个方法绑定到 selectAllElem 复选框,检查是否选中了所有其他复选框。 我将 selectAll() 重命名为 toggleAll() 以显示如何在一种方法中选择/取消选择。

    例如:

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

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

    如果复选框是从数组生成的,您可以使用 scope.options 并在这两种方法中对其进行迭代。

    您可以将输入绑定到范围变量,例如:

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

    然后在函数中设置该变量

    $scope.allSelected = true;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      相关资源
      最近更新 更多