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 ?

br.julien
  • 3,420
  • 2
  • 23
  • 44
JFL
  • 55
  • 4

1 Answers1

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.

Community
  • 1
  • 1
Bricktop
  • 1,549
  • 14
  • 23
  • This Solution doesn't fit with my problem :/ – JFL Jan 23 '15 at 14:57
  • 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. – Bricktop Jan 23 '15 at 15:23