2015-09-22 16 views
0

我想弄清楚如何使用單個角模板處理動態JSON響應。以下是兩個相同模板的示例,但不同的JSON響應(有點類似)。我試圖弄清楚如何在同一個模板中處理這兩個(甚至可能更多)這些響應。 本質上,我如何處理json響應中的UNKNOWN級別?在Angular模板中處理變量JSON響應

http://plnkr.co/edit/1H9AfwUYvcYVEBmBb0Ln?p=preview

{"book": { 
     "title": "Book Title", 
     "chapters": [ 
      { 
       "title": "Chapter One", 
       "units": [ 
        { 
         "title" : "Unit One", 
         "sections": [ 
          {"title" : "Section One"}, 
          {"title" : "Section Two"}, 
          {"title" : "Section Three"} 
         ] 
        }, 
        {"title" : "Unit Two"}, 
        {"title" : "Unit Three"} 
       ] 
      }, 
      {"title": "Chapter Two"}, 
      {"title": "Chapter Three"} 
     ] 
    }}; 

http://plnkr.co/edit/8S6iCrF3A72MNEpKEhMu?p=preview

{"book": { 
     "title": "Book Title", 
     "chapters": [ 
      { 
       "title": "Chapter One", 
       "sections": [ 
          {"title" : "Section One"}, 
          {"title" : "Section Two"}, 
          {"title" : "Section Three"} 
         ] 
      }, 
      {"title": "Chapter Two"}, 
      {"title": "Chapter Three"} 
     ] 
    }}; 

模板:

<div ng-repeat="item in book"> 
    {{item.title}} 
    <ul> 
      <li ng-repeat="chapter in item.chapters"> 
      {{chapter.title}} 
      <ul> 
      <li ng-repeat="unit in chapter.units"> 
       {{unit.title}} 
       <ul> 
       <li ng-repeat="section in unit.sections"> 
        {{section.title}} 
       </li> 
       </ul> 
      </li> 
      </ul> 
     </li> 
     </ul> 
    </div> 
+0

在這裏展示你的代碼中的問題。只顯示一些數據和鏈接不會削減它。我們應該能夠在獨立問題中審查您的問題,而不必離開現場到多個鏈接。這裏真的沒有問題 – charlietfl

+0

@charlietfl更新 – Starboy

+0

指出什麼是你的問題 – andresmijares25

回答

1
<div> 
    {{ book.title }} 
    <ul> 
    <li ng-repeat="chapter in book.chapters" ng-if="book.chapters.length > 0"> 
     {{ chapter.title }} 
     <ul> 
     <li ng-repeat="unit in chapter.units" ng-if="chapter.units.length > 0"> 
      {{ unit.title }} 
      <ul> 
      <li ng-repeat="section in unit.sections"> 
       {{ section.title }} 
      </li> 
      </ul> 
     </li> 
     </ul> 
    </li> 
    </ul> 
</div>