在angularjs中实现复选框

时间:2017-05-01 09:51:43

标签: javascript html css angularjs checkbox

我想在checkbox中使用AngularJs来做出用户的决定。 我无法在checkbox代码中查看AngularJs的状态更改。

我的HTML代码是

<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代码

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

选中checkbox后,$scope.acceptrequest应为true,unchecked $scope.acceptrequest应为false。我怎么能这样做?

我在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;
}

2 个答案:

答案 0 :(得分:1)

为复选框声明ng-model,现在您可以访问控制器内的复选框值。

<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>

控制器:

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

答案 1 :(得分:1)

list添加到复选框,并在ngModel条件

中检查该变量

if
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;
  }
})