angularjs uses ng-change to implement checkbox change trigger events, and uses ng-checked to implement selection but does not trigger the ng-change event. It can only be triggered when manually clicked, which roughly means that only when the checkbox in <td> is directly clicked To trigger add(), add() is invalid when clicking the checkbox in <th> to implement multi-selection in <td>checkbox
The code is as follows
<tr>
<th><input type="checkbox" ng-model="isChecked"></th>
</tr>
<tr ng-repeat="item in data track by $index">
<td><input type="checkbox" ng-checked="isChecked" ng-model="item.isChecked" ng-change="add(item.isChecked,item.uid)"></td>
</tr>
var uid_list = [];
$scope.add = function (item_checked,uid) {
var uid = parseInt(uid);
if(item_checked){
uid_list.push(uid);
}
if(!item_checked){
var index = uid_list.indexOf(uid);
uid_list.splice(index,1);
}
console.log(uid_list);
};
ngChange
If you select all, just process the data directly and extract all IDs. There is no need to call add() to add IDs.
ngChange
Evaluate the given expression when the user changes the input. The expression is evaluated immediately, unlike the JavaScript onchange event which only triggers at the end of a change (usually, when the user leaves the form element or presses the return key).