batuman
batuman

Reputation: 7304

Implementation of checkbox in angularjs

I like to use checkbox in AngularJs for user's decision. I can't catch checkbox's state change in my AngularJs code.

My html code is

<div ng-show="acceptrequest">
     <label class="switchnmn">
        <input type="checkbox" ng-change="stateChanged()" checked>
        <div class="slidernmn round"></div>
    </label>
    <b style="font-size:15px">I am able to accept customer's any request date.</b>
</div>

AngularJs code is

    $scope.stateChanged = function (    ) {
        if(){ //If it is checked
          $scope.acceptrequest = true;
       }
       else
          $scope.acceptrequest = false;
    }

When checkbox is checked, $scope.acceptrequest should be true and unchecked $scope.acceptrequest should be false. How can I do that?

I implemented checkbox is slider in css

.switchnmn {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switchnmn input {display:none;}

.slidernmn {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slidernmn:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slidernmn {
  background-color: #2196F3;
}

input:focus + .slidernmn {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slidernmn:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slidernmn.round {
  border-radius: 34px;
}

.slidernmn.round:before {
  border-radius: 50%;
}

label, b {
  vertical-align: middle;
}

Upvotes: 1

Views: 867

Answers (2)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

add ngModel to checkbox and check that variable inside the if condition

angular.module("app",[])
.controller("ctrl",function($scope){
$scope.checkBtn = true;
$scope.acceptrequest = true;
$scope.stateChanged = function (    ) {
      if($scope.checkBtn){ //If it is checked
        $scope.acceptrequest = true;
     }
     else
        $scope.acceptrequest = false;
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl"> 
<div>
     <label class="switchnmn">
        <input type="checkbox" ng-change="stateChanged()" checked ng-model="checkBtn">
        <div class="slidernmn round"></div>
    </label>
    <b  ng-show="acceptrequest" style="font-size:15px">I am able to accept customer's any request date.</b>  
 
</div>
</div>

Upvotes: 1

Upalr
Upalr

Reputation: 2148

Declare ng-model for the checkbox and now you can access the value of check box inside the controller.

<div ng-show="acceptrequest">
     <label class="switchnmn">
        <input type="checkbox" ng-model="checkBoxValue" ng-change="stateChanged()" checked>
        <div class="slidernmn round"></div>
    </label>
    <b style="font-size:15px">I am able to accept customer's any request date.</b>
</div>

controller:

 $scope.stateChanged = function (    ) {
        if($scope.checkBoxValue){ 
          $scope.acceptrequest = true;
       }
       else
          $scope.acceptrequest = false;
    }

Upvotes: 1

Related Questions