我喜欢在 AngularJs
中使用 checkbox
来让用户做出决定。
我无法在我的 AngularJs
代码中捕捉到 checkbox
的状态变化。
我的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
应该是假的。我该怎么做?
我在 css 中实现了复选框是 slider
.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;
}
最佳答案
为复选框声明ng-model
,现在您可以在 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;
}
关于javascript - angularjs中复选框的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43717231/