Angular js if语句从不规则的json文件中读取

时间:2014-05-08 15:51:14

标签: json angularjs

我有一个类似于以下内容的json文件:

[ { 
    "level" : "1",
    "name" : "TIER 1 - CORE FOUNDATIONS",
    "required" : "13",
    "completed" : "9",
    "planned" : "0",
    "subcategories" : 
        [ { 
            "level" : "2",
            "id" : "39",
            "name" : "Critical Reading and Writing",
            "required" : "6",
            "completed" : "3",
            "planned" : "0",
            "subcategories" : [],
            "courses" : [ 
                {"course_number" : "ENGL-1301", "course_name" : "English", "credits" : "3.0", "grade" : "A"}
              ]
          },
          {
            "level" : "2",
            "id" : "40",
            "name" : "Speaking and Listening",
            "required" : "3",
            "completed" : "3",
            "planned" : "0",
            "subcategories" : [],
            "courses" : [ 
                {"course_number" : "SPCH-1311", "course_name" : "SPCH", "credits" : "3.0", "grade" : "A"}
              ]
          },
          { 
            "level" : "2",
            "id" : "41",
            "name" : "Quantitative Reasoning",
            "required" : "3",
            "completed" : "0",
            "planned" : "0",
            "subcategories" : [],
            "courses" : []
          },
          { 
            "level" : "2",
            "id" : "42",
            "name" : "Wellness and The Human Experience",
            "required" : "3",
            "completed" : "0",
            "planned" : "0",
            "subcategories" : [],
            "courses" : []
          }
        ],
    "courses" : []
  },
  {
    "level" : "1",
    "name" : "TIER 2 - CORE DOMAINS",
    "required" : "26",
    "completed" : "10",
    "planned" : "0",
    "subcategories" : 
        [ { 
            "level" : "2",
            "id" : "27",
            "name" : "Qualitative Reasoning, Literacy and Research",
            "required" : "3",
            "completed" : "3",
            "planned" : "0",
            "subcategories" : [],
            "courses" : [ 
                {"course_number" : "ENGL-1302", "course_name" : "English", "credits" : "3.0", "grade" : "A"}
              ]
          },
          {
            "level" : "2",
            "id" : "32",
            "name" : "Self and Society",
            "required" : "9",
            "completed" : "3",
            "planned" : "0",
            "subcategories" : [
              {
                "level" : "3",
                "id" : "35",
                "name" : "CB080",
                "required" : "3",
                "completed" : "3",
                "planned" : "0",
                "subcategories" : [],
                "courses" : [ 
                    {"course_number" : "PSYC-2301", "course_name" : "General Psychology", "credits" : "3.0", "grade" : "C"}
                  ]
              },
              {
                "level" : "3",
                "id" : "34",
                "name" : "CB070",
                "required" : "9",
                "completed" : "3",
                "planned" : "3",
                "subcategories" : [],
                "courses" : [ 
                    {"course_number" : "GOVT-2305", "course_name" : "Federal Government", "credits" : "3.0", "grade" : "A"}
                  ]
              },
              {
                "level" : "3",
                "id" : "33",
                "name" : "CB060",
                "required" : "9",
                "completed" : "3",
                "planned" : "0",
                "subcategories" : [],
                "courses" : []
              },
          ],
          "courses" : []
          },
          { 
            "level" : "2",
            "id" : "29",
            "name" : "Humanity, Creativity and the Aesthetic Experience",
            "required" : "6",
            "completed" : "3",
            "planned" : "0",
            "subcategories" : [
              {
                "level" : "3",
                "id" : "34",
                "name" : "CB050",
                "required" : "3",
                "completed" : "3",
                "planned" : "0",
                "subcategories" : [],
                "courses" : [ 
                    {"course_number" : "ARTS-1301", "course_name" : "Art Apprec", "credits" : "3.0", "grade" : "A"}
                  ]
              },
              {
                "level" : "3",
                "id" : "34",
                "name" : "CB040",
                "required" : "3",
                "completed" : "0",
                "planned" : "0",
                "subcategories" : [],
                "courses" : [
                    {"course_number" : "ENGL-2328", "course_name" : "American Lit", "credits" : "3.0", "grade" : "A"}
                  ]
              },
            ],
          "courses" : []
          },
          { 
            "level" : "2",
            "id" : "31",
            "name" : "Scientific Discovery and Sustainability",
            "required" : "8",
            "completed" : "4",
            "planned" : "0",
            "subcategories" : [],
            "courses" : [
              {"course_number" : "BIOL-1406", "course_name" : "Biology", "credits" : "4.0", "grade" : "B"}
            ]
          }
        ],
    "courses" : []
  },
  { 
    "level" : "1",
    "name" : "TIER 3 - INTEGRATIVE LEARNING",
    "required" : "3",
    "completed" : "0",
    "planned" : "0",
    "subcategories" : [
        { 
          "level" : "2",
          "id" : "37",
          "name" : "",
          "required" : "3",
          "completed" : "0",
          "planned" : "0",
          "subcategories" : [],
          "courses" : []
        }
    ],
    "courses" : []
  },
  { 
    "level" : "1",
    "name" : "AS.SCIENCE GENERAL ELECTIVES",
    "required" : "18",
    "completed" : "13",
    "planned" : "0",
    "subcategories" : 
        [ { 
            "level" : "2",
            "id" : "25",
            "name" : "",
            "required" : "18",
            "completed" : "13",
            "planned" : "0",
            "subcategories" : [],
            "courses" : [ 
              {"course_number" : "ECON-2302", "course_name" : "Economics", "credits" : "3.0", "grade" : "A"},
              {"course_number" : "COSC-1300", "course_name" : "COSC", "credits" : "3.0", "grade" : "B"},
              {"course_number" : "HDEV-1300", "course_name" : "HDEV", "credits" : "3.0", "grade" : "A"},
              {"course_number" : "CHEM-1405", "course_name" : "Chemistry", "credits" : "4.0", "grade" : "C"},
              {"course_number" : "BIOL-2401", "course_name" : "Biology", "credits" : "--", "grade" : "--"}
            ]
          }
        ],
    "courses" : []
  }
]

在这个json文件中,它有不规则性,即。 level 2中的某些子类别包含level 3个元素,而其他子类别包含courses而不是level 3

如何以角度编写 if 语句,以便在2级子类别下存在3级,然后显示3级课程。否则,2级课程?所以我想知道是否有办法编写ifexists()语句

2 个答案:

答案 0 :(得分:2)

查看ng-if上的文档。

答案 1 :(得分:2)

你可以对子类别使用ng-repeat,如果它是空数组,它将不会显示该子类别。

<div ng-repeat="item in json">
  <h1>{{ item.level }}</h1>
  <ul ng-show="item.courses.length">
     <li ng-repeat="course in item.courses">{{course.course_number}}</li>
  </ul>
  <div ng-repeat="sub in item.subcategories">
    <h2>{{ sub.level }}</h2>
    <ul ng-show="sub.courses.length">
       <li ng-repeat="course in sub.courses">{{course.course_number}}</li>
    </ul>
    <div ng-repeat="sub_sub in sub.subcategories">
       <h3>{{ sub_sub.level }}</h3>
       <ul ng-show="sub_sub.courses.length">
          <li ng-repeat="course in sub_sub.courses">{{course.course_number}}</li>
       </ul>
    </div>
  </div>
</div>