如何使用外部函数取消选中角度复选框?

时间:2016-05-05 09:08:23

标签: javascript jquery angularjs checkbox

基本上我有一组复选框,当它选中时会创建一组标签,但我创建了一个删除标签的功能,问题是,我无法弄清楚如何在删除标签时取消选中该复选框。 / p>

<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);

  };

1 个答案:

答案 0 :(得分:0)

试试这个

<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);
    }
};