2014-03-14 88 views
1

我是Angulrajs的新手,我想在html頁面中列出JSON對象。我想我必須遞歸地進入對象。 Angularjs有什麼可以做的事嗎?遞歸調用Angularjs

我有嵌套的對象。所以,我必須進入這一點。

+0

只使用遞歸,你通常會在JavaScript。 –

回答

2

您將使用ng-repeat來循環您的對象。喜歡這個。

<ul> 
    <li ng-repeat="stuff in things"> stuff </li> 
</ul> 

*編輯後。你認爲你必須使用遞歸,因爲你有嵌套的對象。這並非完全正確。你可以使用多個ng-repeats。像這樣的東西。

<div> 
    <div ng-repeat="stuff in things"> 
    <span> stuff </span> 
    <div ng-repeat="morestuff in stuff"> 
     moreStuff 
    </div> 
</div> 
+0

'ng-repeat'不提供遞歸,只有迭代。 –

+0

是的,這引出了爲什麼需要遞歸的問題。我打賭它不是這種情況。 –

+1

好吧,你可以在評論中詢問它,而不是猜測(即使OP已經明確表示他會*「必須遞歸進入對象」*)。 –

1

如果你還在尋找真正的遞歸,這裏是你能做什麼(例如,遞歸嵌套評論)

<div class="panel panel-default" 
ng-repeat="comment in comments" 
ng-include="'comment_recursion.html'"> 
</div> 

<!-- == Recursion script == --> 
<script type="text/ng-template" id="comment_recursion.html"> 
    <div ng-controller="CommentCtrl"> 
     <div class="panel-heading"> 
      {{comment.author}} 
      <%= render "layouts/controverse_btns" %> 
     </div> 
     <div class="panel-body"> 
      {{comment.text}} 
     </div> 
     <div class="panel-footer"> 
      <span>Created :{{comment.created_at}}, </span> 
      <span>Last Edited : {{comment.updated_at}}</span> 
     </div> 

    </div> 
    <!-- Comment replies (other comments), might want to indent to the right --> 
    <div class="reply" ng-if="comment.replies"> 
     <div class="panel panel-default" 
      ng-repeat="comment in comment.replies" 
      ng-include="'comment_recursion.html'"> 
     </div> 
    </div> 
</script>