ng-repeat unknown number of nested elements

牧云@^-^@ 提交于 2019-11-27 23:08:58

The easiest way is to create a generic partial so you can recursively call and render it using ng-include.

<div ng-include="'partialComment.html'" ng-model="comments"></div>

Here is the partial:

<ul>
    <li ng-repeat="c in comments">
      {{c.text}}
      <div ng-switch on="c.comments.length > 0">
        <div ng-switch-when="true">
          <div ng-init="comments = c.comments;" ng-include="'partialComment.html'"></div>  
        </div>
      </div>
    </li>
</ul>

The data model should be a list var comments = [{ ... }].

I created a demo for you and hope it helps.

Demo

You need a recursive directive. Something like this, or probably better this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!