vzupo vzupo - 5 months ago 13
AngularJS Question

angularjs stop incrementing on uncheck of checkbox

I have a checkbox that adds +2 if the user checks it. I need it to only do this one time. Right now the user can constantly add plus 2 which is not what I need. I need it to add it only once. (factors.Delivery is the ng-model)

I also need it to remove the --2 and stop if the checkmark is removed, but right now, it keeps subtracting.

Any help would be appreciated
Even disabling the check after clicking would be good, but it wont disable after clicking only before.

$scope.factors.Delivery = 0;
$scope.increment = function() {
if ($scope.factors.Delivery >= 0) {
$scope.factors.Delivery+=2;
} else {
$scope.factors.Delivery-=2;
}
};

Answer

2 is greater than or equal to 0. It sounds like the only acceptable values are 2 and 0 based on your post. I would suggest:

$scope.factors.Delivery = 0; 
$scope.increment = function() {
    if ($scope.factors.Delivery == 0) { 
       $scope.factors.Delivery=2;
   } else {
        $scope.factors.Delivery=0;
   }
};`

or using your code, you can disable the checkbox with something like this:

ng-disabled="clickedOnce" on the checkbox and the modifications below.

$scope.factors.Delivery = 0;
$scope.clickedOnce = false;
$scope.increment = function() {
    $scope.clickedOnce = true;
    if ($scope.factors.Delivery >= 0) { 
         $scope.factors.Delivery+=2;
    } else {
        $scope.factors.Delivery-=2;
    }
};
Comments