[英]How can I uncheck a checkbox in angular with an external function?
基本上,我有一组复选框,当其选中时会创建一组标签,但是我创建了一个删除标签的功能,问题是,我无法弄清楚在删除标签时如何取消选中该复选框。
<div class="" ng-repeat="(key, value) in filterTags">{{value}}<div ng-click="changeTag(subproduct);">X</div></div>
<input type="checkbox" ng-model="active" ng-change="change(subproduct, active)">
上面是html,下面是功能
$rootScope.change = function(subproduct, active){
if (active){
$rootScope.filterList.push(subproduct.subproduct_id);
$rootScope.filterPush = "?subproducts[]="+$rootScope.filterList.join("&subproducts[]=");
$rootScope.filterTags.push(subproduct.subproduct_name);
}
else{
$rootScope.filterList.splice($rootScope.filterList.indexOf(subproduct), 1);
$rootScope.filterPush = "?subproducts[]="+$rootScope.filterList.join("&subproducts[]=");
$rootScope.filterTags.splice($rootScope.filterTags.indexOf(subproduct), 1);
}
};
$rootScope.changeTag = function(subproduct, active){
$rootScope.filterList.splice($rootScope.filterList.indexOf(subproduct), 1);
$rootScope.filterPush = "?subproducts[]="+$rootScope.filterList.join("&subproducts[]=");
$rootScope.filterTags.splice($rootScope.filterTags.indexOf(subproduct), 1);
};
试试这个
<input type="checkbox" ng-model="active" ng-change="change(subproduct, active)">
JS
$rootScope.filterTags = [];
$scope.change = function(subproduct, active){
var indexOf = $rootScope.filterTags.indexOf(subproduct.subproduct_name);
if(angular.equals(indexOf, -1)){
$rootScope.filterTags.push(subproduct.subproduct_name);
}
else{
$rootScope.filterTags.splice(indexOf, 1);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.