1

ばかげた質問かもしれませんが、stackoverflow または angular-ui-tree のドキュメントで解決策を見つけられませんでした。

最後に追加されたノードの html 要素を取得する必要があります。

より正確には、新しいノードが作成されたときに表示してフォーカスする必要があるノード テンプレートに xeditable 要素があります。

どうすればできますか。

ご協力いただきありがとうございます。

4

1 に答える 1

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);

};

[STEP 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>
于 2016-02-02T23:58:46.060 に答える