简体   繁体   English

如何清除 ng-repeat 中的所有复选框?

[英]How can I clear all checkboxes from ng-repeat?

I have this method for get the value from objects selected, and the ".length" for indicate at the user how many objects are selected.我有这个方法来从选定的对象中获取值,并且“.length”用于指示用户选择了多少个对象。 but I would like to have a Cancel button and clear all the checkboxes and reset my ".length"但我想要一个取消按钮并清除所有复选框并重置我的“.length”

$scope.selection = [];
$scope.toggleSelection = function toggleSelection(image) {


    var idx = $scope.selection.indexOf(image);
    // is currently selected
    if (idx > -1) {
        $scope.selection.splice(idx, 1);
    }
    // is newly selected
    else {
        $scope.selection.push(image);
    }

    $scope.totalitems = $scope.selection.length;

    $scope.itemobject = {
        'attachments': $scope.selection
    };

this is my ng-repeat:这是我的 ng-repeat:

<ul ng-repeat="image in images | filter:query as attachmensresults">
                        <li>
                            <div class="cbxdelete">
                                <input class="checkbx" id="ckBox" type="checkbox" value="{{image.id}}" ng-checked="selection.indexOf(image.id) > -1" ng-click="toggleSelection(image.id)" />
                            </div>
                            <a href="{{image._links.file.href}}" data-lightbox="example-set" data-title="Click the right half of the image to move forward.">
                                <div class="img-preview">
                                    <img ng-src="{{image._links.file.href}}" alt="" />
                                </div>
                            </a>
                        </li>
                    </ul>

Why not something like this?为什么不是这样的?

<button ng-click="cancel()">Cancel</button>

$scope.cancel = function(){
   $scope.selection = [];
   $scope.totalitems = 0; 
   $scope.itemobject = { 'attachments': $scope.selection };
}

This will reset your selection array to be empty, so ng-checked will be false.这会将您的选择数组重置为空,因此 ng-checked 将为假。

You could also add a selected property to your images array.您还可以将selected属性添加到您的images数组。 This makes changing your model easier because you can use two-way binding with ng-model="image.selected" .这使得更改模型更容易,因为您可以使用ng-model="image.selected"的双向绑定。

To clear all you have to loop over each image.要清除所有内容,您必须遍历每个图像。 Looping over all items is probably a bit slower then your code but I think it's less code and easier to read.循环所有项目可能比您的代码慢一点,但我认为它的代码更少且更易于阅读。

You shouldn't use ids inside of ng-repeat ( id="ckBox" ).您不应该在 ng-repeat ( id="ckBox" ) 中使用 id。 Because ids should be unique in a page.因为 id 在页面中应该是唯一的。

Please have a look at the demo below or at this jsfiddle .请查看下面的演示或此jsfiddle

If you need the selected images as a separate array you could use map to create a new array.如果您需要将所选图像作为单独的数组,您可以使用map创建一个新数组。 See method post as an example.以方法post为例。

 angular.module('demoApp', []) .controller('mainController', MainController); function MainController($filter) { var vm = this, changeAll = function(state) { angular.forEach(vm.images, function(image, index) { image.selected=state; }); }; this.images = [ {id: 1, url: 'http://lorempixel.com/400/200/sports/1/',selected: false}, {id: 2, url: 'http://lorempixel.com/400/200/sports/2/',selected: false}, {id: 3, url: 'http://lorempixel.com/400/200/sports/3/',selected: false} ]; this.cancel = function() { changeAll(false); }; this.selectAll = function() { changeAll(true); }; this.post = function() { var mapped = vm.images.map(function(image,index) { return { id: image.id, selected: image.selected }; }); alert($filter('json')(mapped)); } } MainController.$inject = ['$filter'];
 ul { list-style-type: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="demoApp" ng-controller="mainController as main"> <button ng-click="main.post()">send</button> <button ng-click="main.selectAll()">all</button> <button ng-click="main.cancel()">cancel</button>{{main.images|json}} <ul ng-repeat="image in main.images"> <li> <div class="cbxdelete"> <input class="checkbx" type="checkbox" value="{{image.id}}" ng-model="image.selected" /> </div> <a href="{{image.url}}" data-lightbox="example-set" data-title="Click the right half of the image to move forward."> <div class="img-preview"> <img ng-src="{{image.url}}" alt="" /> </div> </a> </li> </ul> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM