我正在使用这个 Angular TreeView 项目:
https://github.com/nickperkinslondon/angular-bootstrap-nav-tree
我认为这个 treeview 没有在 treeview 上进行搜索的功能,所以我使用一个表单来编写要查找的标签来实现我的。
<form name="searchForm" novalidate style="margin-bottom: 50px">
<div> <input ng-model="search" type="text" class="form-control" placeholder="Buscar..." name="searchTerm" required />
</div>
</form>
这个表单有一个 .watch 来检测用户何时写了一些文本:
$scope.$watch('search', function(newTerm, oldTerm) {
filteredResponse = !newTerm ? response : updateFilteredResponse(normalize(newTerm));
updateTree();
}, true);
函数“updateFilteredResponse”在原始数据集(从 json 读取)中过滤带有包含 newTerm 标签的节点,并返回一个数组,其中包含要在 TreeView 中显示的项目。
“updateTree”函数使用此数组并将我在数组中的自定义项转换为 TreeView 项以添加到 TreeView 。这个项目被添加到 $scope.tree_data = [];
这个数组是使用 abn-tree 指令的数组:
<abn-tree tree-data="tree_data" tree-control="my_tree" ng-if="loaded" expand-level = "2"></abn-tree>
这部分工作正常。当结果 TreeView 显示在屏幕上时,我的问题就来了, TreeView 总是完全折叠。
如果我放置一个类似于库示例代码的按钮,如下所示:
<div style="vertical-align:top">
<button ng-click="my_tree.expand_all()" class="btn btn-default btn-sm">Expand All</button>
</div>
并在 Controller 中声明为示例:
var tree;
$scope.my_tree = tree = {};
当用户点击按钮展开整个搜索结果时,效果很好。我需要在搜索后自动伸展树(Splay Tree) View ,并删除展开所有按钮。
为此,我尝试在我的 Controller 中调用 my_tree.expand_all()。我尝试了不同的调用:
$scope.my_tree.expand_all();
tree.expand_all();
在我的 Controller 和 html 的不同部分(使用 ngIf 和 onload 指令)。即使我试图在 $scope.my_tree 上进行“观察”,以便在准备好 var 时尝试使用 expand_all() 函数,但我总是遇到相同的错误:
$scope.my_tree.expand_all is not a function
有人可以帮我吗?
最佳答案
您可以将 expand_all()
函数放入 setTimeout
函数中,如下所示。
setTimeout(function() {
$scope.my_tree.expand_all();
$scope.$digest();
}, 0);
它必须这样做,因为在将数据绑定(bind)到 treeview 时需要一些延迟时间。
关于javascript - nickperkinslondon angular treeview expand_all() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46832491/