JK_Jha JK_Jha - 1 year ago 102
jQuery Question

How to store valid ng-change event for checkbox in angularjs

I have multiple input text checkbox. I want to store the change box event in an array and later when user submits the Save changes, it should be saved.

<td><input type="checkbox" class="switch" ng-model="each_value.at_enable_flag" ng-change="changeMyArray(each_value)"> </td>

The problem with this is, if a user toggle the check box(On/ Off), it stores both the entries not only the latest one.

I tried to remove the duplicates but that is also having some issue.

Can anyone please help me out how I can store the cehckbox value in an array only if the checkbox original value has changed.

Here is my plunkr. Please have a look and let me know if u need any more details.


Answer Source

Simply store original value in separate property. Then compare with your model.


In your code.

$scope.toTemplate.forEach(function (value) {
      value.default = value.at_enable_flag;

    $scope.myArray = [];

    $scope.saveAtData = function() {
      $scope.myArray = $scope.toTemplate.filter(function (value) {
        return value.default !== value.at_enable_flag;

      $scope.saveValue = $scope.myArray;

There is no need for ng-change on input, as it would fire more than needed. Simply filtering array for changed values is enough.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download