也许这是一个愚蠢的问题,但我还没有在stackoverflow或angular-ui-tree的文档中找到解决方案。
我需要获取最新添加节点的html元素。
更准确地说,我在节点模板中有一个xeditable元素,我需要在创建新节点时显示和聚焦。
我该怎么做。
感谢您的帮助。
答案 0 :(得分:1)
我没有测试过,但我认为应该这样做:
[STEP 1]在您的控制器中:
// Dummy data (please mind the IDs)
$scope.data = [{
'id': 1,
'title': 'node 1',
'type': 'parent', // I like to flag them as well
'nodes': [{
'id' : 11,
'title' : 'node 1.1',
'type': 'child'
}]
}];
// This will add the new item into your stack
$scope.newMenuItem = function() {
$scope.data.push({
id: $scope.data.length + 1,
title: 'test ' + ($scope.data.length + 1),
type: 'parent',
nodes: []
});
// You just pushed the new item into your tree, now we have to somehow reference it
// within our DOM (getting the HTML version of it)
// Please see STEP 2 before reading here - come back once you did that
// ---------------------------------------------------------------------
// Now get the newest element from our tree and convert it to an angular element
var lastItem = $scope.data[$scope.data.length - 1];
// Convert to an angular element (added timeout so we can catch the newly rendered item onto DOM - if no timeout, you can't query the item)
var t = setTimeout(function() {
console.log(angular.element(document.querySelector('#menu-item-' + lastItem['id'])));
clearTimeout(t);
}, 150);
};
[第2步]在您的HTML
中<div ng-controller="DummyController">
<div ui-tree>
<button ng-click="newMenuItem()">Add new item</button>
<ul class="menu" ui-tree-nodes ng-model="data">
<!-- As you can see, I've put the item's ID so I can reference it back into my controller -->
<!-- Now go back to your controller and see how you do that -->
<li ng-repeat="item in data" ui-tree-node data-type="{{item.type}}" id="menu-item-{{item.id}}">
<a href="#">{{item.title}}</a>
</li>
</ul>
</div>
</div>