简体   繁体   中英

How can I check all boxes in a nested ng-repeat by clicking a button?

So what I need to do is get only all of the shown (using ng-show) students checkboxes checked by clicking a toggleAll() button at the top of the page.

Here is my code:

<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">

                ........

I tried using:

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

but length is undefined. Any help would be greatly appreciated!

course is a local variable so it is not accessible on your main scope.

Assuming you want to check all for one course only, you should pass that course into the function.

<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++) {
        ...
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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