angularjs - How to uncheck the checkboxes onclick a button

问题: I have some checkbox which comes from loop in angularjs. Here I need to uncheck all the checkboxes on click a button.Here I have tried already but its not working,can anyon...

问题:

I have some checkbox which comes from loop in angularjs. Here I need to uncheck all the checkboxes on click a button.Here I have tried already but its not working,can anyone please help me on it.Here is the code below

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl">
    <ul ng-repeat="(x, y) in items.filter">
    <li class="parent"><b>{{x}}</b></li>
    <li class="child">
    <ul>
    <li ng-repeat="p in y.value"><input type="checkbox" ng-model="vehicle" name="vehicle">{{p}}</li>
    </ul>
    </li>
    </ul>
    <button ng-click="myFunct()" type="button">click</button> 
    </div>

SCRIPT

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope,$http) {   
$scope.items = {"filter":{"Category1":{"value":["one","two","three"]},"Category2":{"value":["four","five","six"]}}} 
    $scope.myFunct = function() {
  $scope.vehicle="";
    }
    });

回答1:

The combination ng-checked with ng-click is the best solution to control checkboxes from controller, you could try this:

<ul ng-repeat="(x, y) in items.filter">
    <li class="parent"><b>{{x}}</b></li>
    <li class="child">
    <ul>
    <li ng-repeat="p in y.value">
      <input type="checkbox" 
      ng-checked="v"
      name="Item.selected">{{p}}</li>
    </ul>
    </li>
    </ul>
 <input type="checkbox" ng-model="v" ng-click="checkAll()" />Clear all

Example: http://next.plnkr.co/edit/a9DInByf8a6yv6wl?preview

Hope it helps!


回答2:

Assign $scope.vechicle=false

Updated code

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope,$http) 
{   
	$scope.items = {"filter":{"Category1":{"value":["one","two","three"]},"Category2":{"value":["four","five","six"]}}} 
		$scope.myFunct = function() {
		  $scope.vehicle=false; //changed to false
		}
});

  • 发表于 2019-01-07 04:09
  • 阅读 ( 202 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除