【问题标题】:How can I check all boxes in a nested ng-repeat by clicking a button?如何通过单击按钮检查嵌套 ng-repeat 中的所有框?
【发布时间】:2015-09-11 03:08:59
【问题描述】:

所以我需要做的是通过单击页面顶部的 toggleAll() 按钮,仅选中所有显示的(使用 ng-show)学生复选框。

这是我的代码:

<div ng-repeat="course in content" ng-show="course.show" >
    <div ng-repeat="student in course.students" ng-show="student.show" ng-click="toggleStudent(student)">
         <input type="checkbox">

                ........

我尝试使用:

$scope.toggleAll = function () {
        for (var i = 0; i < $scope.course.students.length; i++) {
            ...
        }
    };

但长度未定义。 任何帮助将不胜感激!

【问题讨论】:

  • 请提供一个 JSFiddle 机智大厅的代码?这有助于我们更快/更轻松地进行调试。
  • 全部切换按钮在哪里?
  • 另见this related question(带有主复选框,而不仅仅是一个按钮)。

标签: javascript html angularjs checkbox


【解决方案1】:

course 是一个局部变量,因此无法在您的主范围内访问。

假设您只想检查一门课程的所有内容,您应该将该课程传递给函数。

<div ng-repeat="course in content" ng-show="course.show" >
    <input type='checkbox' ng-click='toggleAll(course)'>
    <div ng-repeat="student in course.students" ng-show="student.show" ng-click="toggleStudent(student)">
        ...


$scope.toggleAll = function (course) {
    for (var i = 0; i < course.students.length; i++) {
        ...
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多