Skip to main content

Update: As of Angular 1.5.x, no more tricks are required, but works only with template, not with templateUrl

Update: As of Angular 1.5.x, no more tricks are required, but works only with template, not with templateUrl

added 21 characters in body
Source Link
sp00m
  • 49k
  • 31
  • 152
  • 263
module.directive("tree", ["RecursionHelper", function(RecursionHelper) {
    return {
        restrict: "E",
        scope: {family: '='},
        template: 
            '<p>{{ family.name }}</p>'+
            '<ul>' + 
                '<li ng-repeat="child in family.children">' + 
                    '<tree family="child"></tree>' +
                '</li>' +
            '</ul>',
        compile: function(element) {
            // Use the compile function from the RecursionHelper,
            // And return the linking function(s) which it returns
            return RecursionHelper.compile(element);
        }
    };
}]);
module.directive("tree", function(RecursionHelper) {
    return {
        restrict: "E",
        scope: {family: '='},
        template: 
            '<p>{{ family.name }}</p>'+
            '<ul>' + 
                '<li ng-repeat="child in family.children">' + 
                    '<tree family="child"></tree>' +
                '</li>' +
            '</ul>',
        compile: function(element) {
            // Use the compile function from the RecursionHelper,
            // And return the linking function(s) which it returns
            return RecursionHelper.compile(element);
        }
    };
});
module.directive("tree", ["RecursionHelper", function(RecursionHelper) {
    return {
        restrict: "E",
        scope: {family: '='},
        template: 
            '<p>{{ family.name }}</p>'+
            '<ul>' + 
                '<li ng-repeat="child in family.children">' + 
                    '<tree family="child"></tree>' +
                '</li>' +
            '</ul>',
        compile: function(element) {
            // Use the compile function from the RecursionHelper,
            // And return the linking function(s) which it returns
            return RecursionHelper.compile(element);
        }
    };
}]);
added 14 characters in body
Source Link
Mark Lagendijk
  • 7k
  • 2
  • 41
  • 31

Inspired by the solutions described in the thread mentioned by @dnc253, I abstracted the recursion functionality into a serviceinto a service.

See this jsFiddlePlunker for a demo. I like this solution best because:

Inspired by the solutions described in the thread mentioned by @dnc253, I abstracted the recursion functionality into a service.

See this jsFiddle for a demo. I like this solution best because:

Inspired by the solutions described in the thread mentioned by @dnc253, I abstracted the recursion functionality into a service.

See this Plunker for a demo. I like this solution best because:

added 772 characters in body
Source Link
Mark Lagendijk
  • 7k
  • 2
  • 41
  • 31
Loading
Source Link
Mark Lagendijk
  • 7k
  • 2
  • 41
  • 31
Loading