从angularjs中的树中获取选中的复选框值

时间:2016-11-19 18:15:24

标签: javascript angularjs

我想知道如何从下面的示例中获取控制器中树的选定复选框值?单击按钮我想显示数组中的所有复选框名称。这是我的plnkr- https://plnkr.co/edit/OSpLLl9YrlzqhM7xsYEv?p=preview //代码在这里,

    //Controller
    Controller to display the tree.
    (function (ng) {
        var app = ng.module('tree', ['tree.service', 'tree.directives']);
        app.controller("TreeController", ["TreeService", function (TreeService) {
            var tc = this;
            buildTree();
            function buildTree() {
                TreeService.getTree().then(function (result) {
                    tc.tree = result.data;
                }, function (result) {
                    alert("Tree no available, Error: " + result);
                });
            }
        }]);
    })(angular);




    //Tree Directive
Directive used for creating tree node.
(function (ng) {
    var app = ng.module('tree.directives', []);
    app.directive('nodeTree', function () {
        return {
            template: '<node ng-repeat="node in tree"></node>',
            replace: true,
            restrict: 'E',
            scope: {
                tree: '=children'
            }
        };
    });
    app.directive('node', function ($compile) {
        return {
            restrict: 'E',
            replace: true,
            templateUrl: 'node.html', // HTML for a single node.
            link: function (scope, element) {
                /*
                 * Here we are checking that if current node has children then compiling/rendering children.
                 * */
                if (scope.node && scope.node.children && scope.node.children.length > 0) {
                    scope.node.childrenVisibility = true;
                    var childNode = $compile('<ul class="tree" ng-if="!node.childrenVisibility"><node-tree children="node.children"></node-tree></ul>')(scope);
                    element.append(childNode);
                } else {
                    scope.node.childrenVisibility = false;
                }
            },
            controller: ["$scope", function ($scope) {
                // This function is for just toggle the visibility of children
                $scope.toggleVisibility = function (node) {
                    if (node.children) {
                        node.childrenVisibility = !node.childrenVisibility;
                    }
                };
                // Here We are marking check/un-check all the nodes.
                $scope.checkNode = function (node) {
                    node.checked = !node.checked;
                    function checkChildren(c) {
                        angular.forEach(c.children, function (c) {
                            c.checked = node.checked;
                            checkChildren(c);
                        });
                    }

                    checkChildren(node);
                };
            }]
        };
    });
})(angular);

1 个答案:

答案 0 :(得分:0)

你好:看看这个plunker链接。它在这里工作

https://github.com/firebase/firebaseui-android#using-the-library-in-your-android-app

(function (ng) {
var app = ng.module('tree', ['tree.service', 'tree.directives']);
app.controller("TreeController", ["TreeService", "$scope", function (TreeService, $scope) {
    var tc = this;
    buildTree();
    function buildTree() {
        TreeService.getTree().then(function (result) {
            tc.tree = result.data;
        }, function (result) {
            alert("Tree no available, Error: " + result);
        });
    }


   $scope.selectedItems = [];

   $scope.getSelected = function(){
     $scope.selectedItems = [];
     function checkChildren(c) {
          angular.forEach(c.children, function (c) {
             if (c.checked){
                $scope.selectedItems.push({"selected":c.name});
             }
              checkChildren(c);
          });
     }


      angular.forEach(tc.tree, function(value, key) {
          if (value.checked){
            $scope.selectedItems.push({"selected":value.name});
          }

           checkChildren(value);
      });
   };
}]);})(angular);