本文介绍了嵌套元素NG重复数目不详的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果有一个简单的解决此类问题。

I'm wondering if there is a simple solution to this type of problem.

我有一个对象评论,这反过来又会包含注释,这些注释也可以包含注释...这可以去一个未知的周期数。

I have an object comment, which can in turn contain comments, and those comments can also contain comments ...and this can go on for a unknown number of cycles.

下面是数据结构的示例:

Here is an example of the data structure:

var comment = {
   text : "",
   comments: [
      { text: "", comments : []},
      { text: "", comments: [
         { text: "", comments : []},
         { text: "", comments : []}
         { text: "", comments : []}
      ]}
   ]
}

比方说2级的意见,我会写:

Lets say for 2 levels of comments I would write:

<div ng-repeat="comment in comments">
   {{comment.text}}
   <div ng-repeat="comment in comments">
      {{comment.text}}
   </div>
</div>

我将如何implent我的div的嵌套评论N的水平?

How would I implent my divs for "n" level of nested comments ?

推荐答案

最简单的方法是创建一个通用的部分,所以你可以递归调用,并使用使其 NG-包括

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>

下面是部分:

<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>

数据模型应该是一个List VAR评论= [{...}]

我为您创建一个演示,并希望它能帮助。

I created a demo for you and hope it helps.

这篇关于嵌套元素NG重复数目不详的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:29