简体   繁体   English

所有选定项目中的角度取消选择项目

[英]Angular unselect item in all selected items

Below is my code. 下面是我的代码。 When user clicks on "select all" all items get selected. 当用户单击“全选”时,所有项目均被选中。

what I want is when user click on any option that option should be disabled. 我想要的是当用户单击任何应禁用该选项的选项。

 var app = angular.module('sampleApp', []); app.controller('sampleController', ['$scope', function($scope) { $scope.selectAll = false; } ]) 
 .disabled { opacity: 0.5 } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="sampleApp"> <div ng-controller="sampleController"> <label ng-click="selectAll = !selectAll">Select all</label> <div ng-class="selectAll ? 'enable' : 'disabled'">Option 1</div> <div ng-class="selectAll ? 'enable' : 'disabled'">Option 2</div> <div ng-class="selectAll ? 'enable' : 'disabled'">Option 3</div> <div ng-class="selectAll ? 'enable' : 'disabled'">Option 4</div> </div> </div> 

You need to wrap each item into a new object and place a field 'selected' on this object: 您需要将每个项目包装到一个新对象中,并在此对象上放置一个“选定”字段:

 var app = angular.module('sampleApp', []); app.controller('sampleController', ['$scope', function($scope) { $scope.options = [{ selected: false, item: 'option 1' }, { selected: false, item: 'option 2' }, { selected: false, item: 'option 3' }] $scope.selectAll = function(select) { angular.forEach($scope.options, function(o) { o.selected = select; }); }; } ]) 
 .disabled { opacity: 0.5 } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="sampleApp"> <div ng-controller="sampleController"> <label ng-click="selectAll(true)">Select all</label> <label ng-click="selectAll(false)">Deselect all</label> <div ng-repeat="option in options" ng-class="{'disabled': !option.selected}" ng-click="option.selected = !option.selected">{{option.item}}</div> </div> </div> 

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

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