1

How can I build the following structure using ng-repeat? Solution must support an arbitrary depth...

I've played with ng-repeat-start & ng-repeat-end but without any luck so far. Any hint is greatly appriciated

<div>1
   <div>2
       <div>3</div>
   </div>
</div>
1

1 Answer 1

1

It can be accomplished through a directive that leverages ng-repeat to render values from nested objects. See it working here.

Directive

app.directive('divRepeater', function($compile) {
  return {
    restrict: 'E',
    replace: true,
    template: '<ul></ul>',
    scope: {
      obj: '='
    },
    link: function(scope, element) {
      var el = angular.element('<span/>');
      el.append('<li>' + scope.obj.id + '</li>');
      if (scope.obj.nestedObjs) {
        var nestedObjs = angular.toJson(scope.obj.nestedObjs);

        /// remove quotes from property names
        nestedObjs = nestedObjs.replace(/\"([^(\")"]+)\":/g, "$1:");

        var nestedDir = "<div ng-init='nestedObjs=" + nestedObjs + "'><div-repeater ng-repeat='nestedObj in nestedObjs track by $index' obj='nestedObj'></div-repeater></div>";
        el.append(nestedDir);
      }
      $compile(el)(scope);
      element.append(el);
    }
  };
});

Controller

app.controller('MainCtrl', function($scope) {
  $scope.objs = [{
    id: '1',
    nestedObjs: [{
      id: 'a'
    }, {
      id: 'b',
      nestedObjs: [{
        id: 'i'
      }, {
        id: 'ii'
      }]
    }]
  }, {
    id: '2'
  }, {
    id: '3',
    nestedObjs: [{
      id: 'a',
      nestedObjs: [{
        id: 'i'
      }]
    }, {
      id: 'b',
      nestedObjs: [{
        id: 'i'
      }, {
        id: 'ii'
      }]
    }]
  }];
});

Markup

<div-repeater ng-repeat="obj in objs track by $index" obj="obj"></div-repeater>
Sign up to request clarification or add additional context in comments.

Comments

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.