1

I try to build my menue with n sub menues with AngularJS. this is my scope:

$scope.menu = [
                    {"type": "folder", "name": "TestFolder1", "subfolder": []},
                    {"type": "folder", "name": "TestFolder2", "subfolder": [
                        {"type": "folder", "name": "TestFolder2", "subfolder": [
                            {"type": "folder", "name": "TestFolder2", "subfolder": []},
                            {"type": "folder", "name": "TestFolder2", "subfolder": []}
                        ]},
                        {"type": "folder", "name": "TestFolder2", "subfolder": []}
                    ]},
                    {"type": "file", "name": "testfile"}
            ];

this is my directive

codeApp.directive('item', ['$compile', function ($compile) {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                item: '='
            },
            template: '<li>'+
            '<a id="item"><i class="fa fa-code-fork fa-fw"></i>{{item.name}}</a>'+
            '</li>',
            link: function($scope, $element) {
                if (angular.isArray($scope.item.subfolder) && $scope.item.subfolder.length > 0) {
                    $element.append('<ul><item ng-repeat="childItem in item.subfolder" item="childItem"></item></ul>');
                    $compile($element.contents())($scope);
                }
            }
        };
    }]);

and my first line is this :

<item ng-repeat="item in menu" item="item"></item>

The menu is created, but only the first level and I'm not sure how the compile function works. How can I achieve that the line is appended after the ?

1 Answer 1

1

I think a similar problem has been discussed here: https://stackoverflow.com/a/18609594/4360457

The answer includes a plunkr example and is explained very well.

Sign up to request clarification or add additional context in comments.

2 Comments

This Solution doesn't fit with my problem :/
Could you rephrase your question then? The answer I linked to explains how to build a tree from a javascript object using recursion within directives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.