Я пытаюсь понять, как обрабатывать динамические ответы JSON с помощью одного углового шаблона. Ниже приведены два примера одного и того же шаблона, но разные ответы JSON (несколько схожие). Я пытаюсь понять, как обрабатывать оба (и, возможно, больше) этих ответов в одном шаблоне. По сути, как мне обрабатывать уровни UNKNOWN в ответ 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>
<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>