javascript - 使用 ng-repeat 的 Angular 复选框值

标签 javascript angularjs

我是angularjs的新手。所以,我有一个包含一些值的数组,我想在复选框中使用它。所以, HTML代码

<div class="col-xs-12">
                    <div class="form-group">
                    <label class="col-xs-3 control-label" for="domain">Domain</label>
                        <div class="col-xs-4">
                            <div class="multiselect">
                                <div class="selectBox" ng-click="showCheckboxes()">
                                <select>
                                    <option>Select an option</option>
                                </select>
                                <div class="overSelect"></div>
                                </div>
                                <div id="checkboxes">
                                <label for="one">
                                    <input type="checkbox" id="one" />First checkbox</label>
                                <label for="two">
                                    <input type="checkbox" id="two" />Second checkbox</label>
                                <label for="three">
                                    <input type="checkbox" id="three" />Third checkbox</label>
                                </div>
                            </div>
                    </div>
                </div>
            </div>

我的数组就像 -

$scope.tempval = [{ name: 'ABC' }, { name: 'PQR' }, { name: 'XYZ' }, { name: 'EFG' }];

所以,这里 我想显示 chekbox 的这些值(value),我使用 ng-repeat 尝试了不同的事情,但我无法做到这一点,所以有人可以帮助我吗?提前致谢。

CSS 是

.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}

Controller -

var.expand = false
$scope.showCheckboxes = function(){
                    var checkboxes = document.getElementById("checkboxes");
                                if (!expanded) {
                                    checkboxes.style.display = "block";
                                    expanded = true;
                                } else {
                                    checkboxes.style.display = "none";
                                    expanded = false;
                                }
                };

我还想获取数组中所有选中的值。我该怎么做?

最佳答案

试试这个:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
  var expand = false
  $scope.tempval = [{ name: 'ABC' }, { name: 'PQR' }, { name: 'XYZ' }, { name: 'EFG' }];
  $scope.showCheckboxes = function() {
    var checkboxes = document.getElementById("checkboxes");
    if (!expand) {
      checkboxes.style.display = "block";
      expand = true;
    } else {
      checkboxes.style.display = "none";
      expand = false;
    }
  };
});
.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl" class="col-xs-12">
    <div class="selectBox" ng-click="showCheckboxes()">
        <select>
               <option>Select an option</option>
        </select>
        <div class="overSelect"></div>
    </div>
    <div id="checkboxes">
        <label  ng-repeat="item in tempval" for="{{item.name}}">
            <input type="checkbox" id="{{item.name}}" />{{item.name}}
        </label>
    </div>
</div>

关于javascript - 使用 ng-repeat 的 Angular 复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515176/

相关文章:

javascript - 使用参数指定 Breeze 实体默认资源名称

javascript - 使用 JSON 中的值填充选择不起作用

javascript - 将背景图片设置为 100%

javascript - 响应式导航干扰其他代码元素

javascript - 如何隐藏 HTML 元素不出现在仅用于桌面的其他页面上?

javascript - Vue props 变成 null 了?

node.js - 在 typescript 中使用带有 Angular 6 的 Node 短 ID 模块

javascript - ng-check ="true" radio 选择更新时不工作

javascript - Uncaught ReferenceError : jasmineRequire is not defined

javascript - for 循环中的 If 语句被忽略