【问题标题】:Angularjs issue with directive unchecking all checkboxesAngularjs问题与指令取消选中所有复选框
【发布时间】:2017-01-06 22:32:07
【问题描述】:

您好,感谢您查看我的问题。

我正在创建一个简单列出父母及其子女的指令。每个父级都有一个可用的复选框,允许用户打印确实选中的父级(例如:如果用户检查父级#1 和#5,则仅打印#1 和#5)。我还有一个过滤器,允许用户“过滤”父母(超过 100 个)。

问题在于复选框:我希望过滤器(下拉菜单)在被选中时会清除所有复选框。现在,如果用户检查父级 #1 和 #5 并过滤掉 5,则 #1 保持选中状态。我在下面添加我的代码。 非常感谢您帮助我...

app.directive('isolateScopeWithController', function () {

var controller = ['$scope', 'treeFactory', '$q', function ($scope, treeFactory, $q) {

    var vm = this;

    $scope.ocw = "";

    $scope.types = [
                   {description : "All Parent", type : 2},
                   {description : "True Parent", type : 1},
                   {description : "Parent", type : 0}
               ];

     $scope.$watch('selectedType', function(){          
        // I wish I would have this function working
        uncheckAll();

    })

   function uncheckAll() {
        // uncheck all the  check box currently checked 
        // cannot do it!
    }

    init();

    function init(){

        $scope.selectedType = "2";

        $scope.ocw = {
         "parents":[
         {name: "parent1", "type": 0, children: ["name": "child1", "name": "child2"]},
         {name: "parent2", "type": 0, children: ["name": "child3", "name": "child4"]},
         ...
         {name: "parent5", "type": 1, children: ["name": "child10", "name": "child11"]}
         ]
        }
    }
    ],

  template = '<div>'+
                 '<div>'+
                    '<select ng-model="selectedType" ng-change="update(selectedType)">'+
                        '<option ng-repeat="x in types" value="{{x.type}}">{{x.description}}</option>'+
                    '</select>'+                        
                 '</div>'+                      
                 '<div>'+
                    '<button type="button" class="btn btn-default btn-primary" ng-click="printParentschecked(ocw)"> <span class="glyphicon glyphicon-print"></span> Print Selected </button>'+
                 '</div>'+
             '</div>'+
                 '<div ng-repeat="parent in ocw.parents" class="col-sm-8">'+
                    '<div ng-if="filterType(parent.type)">'+    
                         '<div class="row">'+
                            '<div class="col-sm-8">'+
                                '<span>{{parent.name}}</span>'+
                            '</div>'+
                            '<div class="col-sm-2">'+
                                '<input type="checkbox" ng-model="parent.isOpenValue" ng-click="checked(parent.name)" >'+
                            '</div>'+
                         '</div>'+
                        '<div ng-repeat="org in parent.children" class="col-sm-8">'+
                            '{{children.name}}'+
                        '</div>'
                    '</div>'+       
                 '</div>'+

             '</div>';
  return {
      restrict: 'E', 
      replace: true,
      scope: {},
      controller: controller,
      template: template,   
      transclude: true,
      link: function(scope, element, attrs, transclude) {
                scope.printParentschecked = function(ocw){
                    // print the checked parents... I can do it!
                }
                scope.filterType = function(type) {
                    //it works, it checks the value selected in the drop down and the type of the parent
                    // if they match they are shown
                if (types === 1 && type === 1) {
                    return true;
                }
                ....


          }  
      }
  };
});

附:可能真正的问题是我不太了解角度指令:(

【问题讨论】:

  • init() 定义后似乎有语法错误 - 有 ],,它可能应该是 }]; 作为 controller 定义的结尾。这可能无法解决您的主要问题,但您仍然需要语法正确的代码才能让 any 解决方案真正发挥作用。

标签: angularjs


【解决方案1】:

试试这个:

  function uncheckAll() {
              angular.forEach(ocw.parents,function(parent,key){                  
                       parent.isOpenValue=false;        
                      }
            }

【讨论】:

  • 很好的答案斯里哈里!四行拯救了我的一天!谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-09-09
  • 2016-02-17
  • 2014-11-19
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
  • 2011-06-15
相关资源
最近更新 更多