javascript - angularjs中复选框的实现

标签 javascript html css angularjs checkbox

我喜欢在 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/

相关文章:

javascript - 如何在 Rails 4 中使用 jquery 删除 <p> 标签

javascript - 使用 jQuery 计算精确的窗口高度

html - 使页脚被内容推离页面,但在不是整页内容时留在底部

java - 如果我将 CSS 文件夹放入 WEB-INF 文件夹中,我的 CSS 不会显示

javascript - 如何在页面之间共享 JS 变量?

html - 在较小的容器中制作一半页面宽度的图像

javascript - 如何在 Javascript 的数组中获取字符串的订单号?

javascript - 具有 .css() 连接问题的 jQuery 变量?

Javascript 拖放 : ondragleave working solution?

php - DIV 视频仅在第一次加载首页时显示