简体   繁体   English

在Angular模板中处理变量JSON响应

[英]Handling variable JSON response in Angular template

I'm trying to figure out how to handle dynamic JSON responses with a single angular template. 我正在尝试弄清楚如何使用单个角度模板来处理动态JSON响应。 Below are two examples of the same template, but different JSON responses (somewhat similar). 以下是相同模板但具有不同JSON响应(有点相似)的两个示例。 I'm trying to figure out how to handle both (and quite possibly more) of these responses in the same template. 我试图弄清楚如何在同一个模板中处理两个(甚至更多)这些响应。 Essentially how do I handle UNKNOWN levels in a json response? 本质上,我该如何处理json响应中的UNKNOWN级别?

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

{"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 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"}
        ]
    }};

Template: 模板:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM