Control Checkbox in angularJS

0

I have a checkbox and I want to control its action from the controller when false or true .

here is the Html :

<md-checkbox ng-model="checkstate" aria-label="Checkbox 1" ng-true-value="'prioritario'" ng-false-value="'Normal'">
     {{checknull}}
</md-checkbox>

"The end is that the controller controls the action."

When logearme I receive a JSON: id_servicio: "38,39" , I separate them with a split and I store them in localStorage staying in:

  • LoginData.getData (). service_id.split (",") [0] ,
  • LoginData.getData (). service_id.split (",") [1]

Then in the controller I make a request to my DB and in then I do the following:

.then(function(data) {

    var dat = data.data;
    $scope.tickets = [];


    $scope.verificarCheckbox = function() {

        if ($scope.checkstate) {

            for (var i = 0; i < dat.length; i++) {
                dat[i]
                var ticket = {
                    numero: dat[i].numero,
                    rut: dat[i].rut,
                    servicio_id: LoginData.getData().id_servicio.split(",")[0]
                };
                $scope.tickets.push(ticket);
            }
        } else {

            for (var i = 0; i < dat.length; i++) {
                dat[i]
                var ticket = {
                    numero: dat[i].numero,
                    rut: dat[i].rut,
                    servicio_id: LoginData.getData().id_servicio.split(",")[1]
                };
                $scope.tickets.push(ticket);
            }
        }
    }
});

The idea is that if I select the checkbox send the fix [0] and if I de-select it send the fix [1]

    
asked by Hernan Humaña 02.11.2016 в 16:05
source

1 answer

1

You can occupy the event ng-change

<md-checkbox ng-model="checkstate" ng-change="verificarCheckbox()" aria-label="Checkbox 1" ng-true-value="'prioritario'" ng-false-value="'Normal'">
    {{checknull}}
</md-checkbox>

And from your controller

$scope.verificarCheckbox = function(){
    if($scope.checkstate){
       //mande el arreglo[0]
    }else{
       //mande el arreglo[1]
    }
}

EDIT

Besides adding my answer I advise you to improve your code, you have two identical blocks except for 1 value that is your value of the arrangement that depends on the checkbox you could do the following

.then(function(data) {

    var dat = data.data;
    $scope.tickets = [];


    $scope.verificarCheckbox = function() {
        var servicio_id = LoginData.getData().id_servicio.split(",")[0];
        if (!$scope.checkstate) {
            servicio_id = LoginData.getData().id_servicio.split(",")[1];
        }
        for (var i = 0; i < dat.length; i++) {
            dat[i]
            var ticket = {
                numero: dat[i].numero,
                rut: dat[i].rut,
                servicio_id: servicio_id
            };
            $scope.tickets.push(ticket);
        }
    }
});

With this we give a value to a variable called servicio_id that by default is the [0] , we verify if the checkbox is selected or not. If it is NOT, we assign the value [1] .

    
answered by 02.11.2016 / 16:13
source