2017-04-13 32 views
0

对于Angular UI树,我想添加expland all或全部折叠。如何折叠每个有角度的UI树上的所有功能

我试过下面的代码,它的工作原理。

$scope.collapseAll = function() { 
     $scope.$broadcast('angular-ui-tree:collapse-all'); 
     }; 

    $scope.expandAll = function() { 
    $scope.$broadcast('angular-ui-tree:expand-all'); 
    }; 
    ... 

问题是,我的页面有多个UI树,我只想触发expandAll函数单独。现在我触发这个expandAll函数。 全部 ui树会受到影响。

有没有解决方案将每一个设置为单独的?

How to call collapseAll function on Angular UI tree?

回答

1

如果选中在角UI树Source,在'angular-ui-tree:collapse-all'事件触发一个范围值。其他问题是angular-ui-tree中的每个指令都会继承父级的范围,而不是具有隔离范围。因此,改变广播事件时,它实际上正在改变相同的范围值。

你应该找到一种方法让每棵树有一个单独的作用域,或者考虑使用其他树例如v-accordion

+1

谢谢你的回复。 V型手风琴是一个奇妙的插件,后来会考虑使用它。无论如何,我使用while循环并递归获取childNode,可以解决它。谢谢 –

0

我们应该为指定的树作用域广播angular-ui-tree:collapse-all。 它适用于我,你可以毫不犹豫地使用它。

function collapseAll() { 
     var treeScope = _getRootNodesScope('myTreeId'); 
     if (treeScope) { 
      treeScope.$broadcast('angular-ui-tree:collapse-all'); 
     } 
    } 


function expandAll() { 
     var treeScope = _getRootNodesScope('userTreeroot'); 
     if (treeScope) { 
      treeScope.$broadcast('angular-ui-tree:expand-all'); 
     } 
    } 

    function _getRootNodesScope(myTreeId) { 
var treeElement = angular.element(document.getElementById(myTreeId)); 
if (treeElement) { 
var treeScope = (typeof treeElement.scope === 'function') ? 
treeElement.scope() : undefined; 
      return treeScope; 
     } 
     return undefined; 
    };