0

I have created an applicaion in angular for getting all the checkbox value which is checked into an array, without any looping, but after simultaneous checking and un-checking i am getting wrong data within the array

Working Demo

can anyone please give me some solution for this

var app = angular.module('checkbox', []);

app.controller('homeCtrl', function ($scope) {
    $scope.selected = [];
    $scope.array_ = angular.copy($scope.array);
    $scope.list = [{
        "id": 1,
            "value": "apple",
            "checked": false
    }, {
        "id": 3,
            "value": "orange",
            "checked": false
    }, {
        "id": 5,
            "value": "pear",
            "checked": false
    }];
    $scope.checkedOrNot = function (id, isChecked, index) {
        if (isChecked) {
            $scope.selected.push(id);
        } else {
            $scope.selected.splice(index, 1);
        }
    }
});

1 Answer 1

3

please see here http://jsfiddle.net/7L6beac6/2/

you need to find index of checkbox in selected array and remove it using that index instead of index of checkbox inside reaper

var app = angular.module('checkbox', []);

app.controller('homeCtrl', function ($scope) {
    $scope.selected = [];
    $scope.array_ = angular.copy($scope.array);
    $scope.list = [{
        "id": 1,
            "value": "apple",
            "checked": false
    }, {
        "id": 3,
            "value": "orange",
            "checked": false
    }, {
        "id": 5,
            "value": "pear",
            "checked": false
    }];
    $scope.checkedOrNot = function (id, isChecked, index) {
        console.log("index:" + index + " " + isChecked);

        if (isChecked) {
            $scope.selected.push(id);
        } else {
            var _index = $scope.selected.indexOf(id);
            $scope.selected.splice(_index, 1);
        }
    };
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.