javascript - In Angularjs, if all checkboxes are selected without manual click, ng-change will not be triggered?
迷茫
迷茫 2017-06-14 10:54:09
0
2
699

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);
    };
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
我想大声告诉你

ngChange
If you select all, just process the data directly and extract all IDs. There is no need to call add() to add IDs.

过去多啦不再A梦

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).

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!