search

Home  >  Q&A  >  body text

javascript - In Angularjs, if all checkboxes are selected without manual click, ng-change will not be triggered?

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

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-14 10:56:09

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

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-14 10:56:09

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

    reply
    0
  • Cancelreply