我有一个通过一些JSON在Angular中构建的菜单,这个菜单最多可以达到3级。有些菜单项没有孩子,有些则进入第二或第三级。我想找出一种方法来确定一个选定的子类别是否有任何子元素,如果有,那么我需要隐藏一个按钮。
每次选择子类别时,如#34;数字艺术"该值在$ scope.selectedSubMarket;
中捕获我如何在$ scope.art中检查以查看带有cat:Digital Art的对象是否有任何子对象?在JSON中你会看到一个类似于' Comics'的子类别。没有2级,我需要一种方法来检查这个并且没有运气....
以下是构建菜单的JSON示例:
$scope.art = [
{"cat": "Digital Art", "second": [
{"sub": "3 Dimensional Art", "third": [
{"desc": "Abstract"},
{"desc": "Characters"},
{"desc": "Objects"},
{"desc": "Scenes"},
{"desc": "Vehicles"},
]},
{"sub": "Animation", "third": [
{"desc": "Abstract"},
{"desc": "Characters"},
{"desc": "Animals"},
{"desc": "Fantasy"},
{"desc": "Graffiti"},
{"desc": "Illustrations"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Space Art"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Still Life"},
{"desc": "Surreal"},
]},
{"sub": "Fractal Art", "third": [
{"desc": "Fractal Animation"},
{"desc": "Fractal Manipulation"},
{"desc": "Raw Fractal"},
]},
{"sub": "Photomanipulation", "third": [
{"desc": "Abstract"},
{"desc": "Animals & Plants"},
{"desc": "Conceptual"},
{"desc": "Dark"},
{"desc": "Emotional"},
{"desc": "Humourous"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Minimalism"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Surreal"},
]},
{"sub": "Pixel Art"},
{"sub": "Text Art"},
{"sub": "Typography"},
{"sub": "Vector", "third": [
{"desc": "Abstract"},
{"desc": "Animals & Plants"},
{"desc": "Conceptual"},
{"desc": "Dark"},
{"desc": "Emotional"},
{"desc": "Humourous"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Minimalism"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Surreal"},
]},
{"sub": "Vexel", "third": [
{"desc": "Abstract"},
{"desc": "Animals & Plants"},
{"desc": "Conceptual"},
{"desc": "Dark"},
{"desc": "Emotional"},
{"desc": "Humourous"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Minimalism"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Surreal"},
]},
]},
{"cat": "Traditional Art", "second": [
{"sub": "Assemblage"},
{"sub": "Body Art", "third": [
{"desc": "Body Modification"},
{"desc": "Cosmetic Application"},
{"desc": "Tattoos"},
{"desc": "Scenes"},
{"desc": "Vehicles"},
]},
{"sub": "Collage"},
{"sub": "Drawings", "third": [
{"desc": "Abstract"},
{"desc": "Technical Drawing"},
{"desc": "Animals"},
{"desc": "Fantasy"},
{"desc": "Portrait & Figures"},
{"desc": "Illustrations"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-Fi"},
{"desc": "Space Art"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Still Life"},
{"desc": "Surreal"},
]},
{"sub": "Installation Art"},
{"sub": "Scratchboard"},
{"sub": "Paintings", "third": [
{"desc": "Abstract"},
{"desc": "Surreal"},
{"desc": "Animals"},
{"desc": "Fantasy"},
{"desc": "Portrait & Figures"},
{"desc": "Illustrations"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-Fi"},
{"desc": "Space Art"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Still Life"},
{"desc": "Surreal"},
]},
{"sub": "Printing", "third": [
{"desc": "Collagraph"},
{"desc": "Dry Point"},
{"desc": "Etching"},
{"desc": "Lino"},
{"desc": "Lithograph"},
{"desc": "Monotype"},
{"desc": "Silk Screen"},
{"desc": "Wood Block"},
]},
{"sub": "Street Art", "third": [
{"desc": "Freehand Graffiti"},
{"desc": "Installations"},
{"desc": "Sketch Design"},
{"desc": "Stencils"},
{"desc": "Stickers"},
{"desc": "Wheatpasting"},
]},
{"sub": "Sculpture", "third": [
{"desc": "Abstract"},
{"desc": "Busts(People)"},
{"desc": "Fantasy"},
{"desc": "Figurative"},
{"desc": "Horror"},
{"desc": "Lego"},
{"desc": "Models"},
{"desc": "Surreal"},
]},
]},
{"cat": "Literature"},
{"cat": "Artisan Craft"},
{"cat": "Cartoons"},
{"cat": "Comics"},
{"cat": "Animation"},
{"cat": "Manga & Anime"},
{"cat": "Scraps"},
{"cat": "Anthro"},
{"cat": "Fan Art"},
]
答案 0 :(得分:1)
如果我找对你
var element = $scope.art.filter(function (el) {return el["cat"] === "Digital Art";})[0];
if (element.second && element.second.length) {
// Do something
};
对于子元素,请使用上一代码中的element
:
var subElement = element["second"].filter(function (el) {return el["sub"] === "Animation";})[0];
if (subElement.third && subElement.third.length) {
// Do something
};