1

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;
}
0

2 Answers 2

1

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;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks let me check.
If I change state, the whole checkbox disappear.
yes, that's for sure because your chekbox is inside the div('<div ng-show="acceptrequest">') and you make the div hide show using the checkbox
1

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>

2 Comments

If I change state, the whole checkbox disappear.
The same thing happened at your snippet.

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.