Javascript 使用Angular ng repeat访问JSON的第三级
我正在使用在提供的过滤器来运行JSON。但是,我需要访问所有3rd-level元素,而不是像这里的示例那样访问第2级 我的JSON看起来像:Javascript 使用Angular ng repeat访问JSON的第三级,javascript,json,angularjs,Javascript,Json,Angularjs,我正在使用在提供的过滤器来运行JSON。但是,我需要访问所有3rd-level元素,而不是像这里的示例那样访问第2级 我的JSON看起来像: [ { "category": "colors", "heading": "Colors & Background", "indexes": [ { "index": "colors", "name": "Colors", "patter
[
{
"category": "colors",
"heading": "Colors & Background",
"indexes": [
{
"index": "colors",
"name": "Colors",
"patterns": [
{
"index": "background-patterns",
"name": "Background Patterns"
}
]
}
],
"order": "1"
},
{
"category": "typography",
"heading": "Typography",
"indexes": [
{
"index": "typography",
"name": "Typography",
"patterns": [
{
"index": "headings",
"name": "Headings"
},
{
"index": "plain-text",
"name": "Plain Text"
},
{
"index": "text-icon",
"name": "Text Icon"
}
]
}
],
"order": "2"
}
]
var app = angular.module('mymod', []);
app.filter('createarray', function () {
return function (value, propertyName) {
var arrayList = [];
angular.forEach(value, function (val) {
angular.forEach(val[propertyName], function (v) {
arrayList.push(v)
});
});
console.log(arrayList)
return arrayList;
}
});
app.directive('everything', function() {
return {
restrict: 'E',
templateUrl: 'everything.html',
controller: function($scope, $http) {
$http.get('assets/js/test.json')
.then(function(result) {
$scope.everything = result.data;
});
},
controllerAs: 'everything'
}
});
<nav class="om-nav-everything">
<ul>
<li ng-repeat="pattern in everything.indexes | createarray: 'patterns'"><a href="index-{{pattern.index}}.html">{{pattern.name}}</a></li>
</ul>
</nav>
我的app.js看起来像:
[
{
"category": "colors",
"heading": "Colors & Background",
"indexes": [
{
"index": "colors",
"name": "Colors",
"patterns": [
{
"index": "background-patterns",
"name": "Background Patterns"
}
]
}
],
"order": "1"
},
{
"category": "typography",
"heading": "Typography",
"indexes": [
{
"index": "typography",
"name": "Typography",
"patterns": [
{
"index": "headings",
"name": "Headings"
},
{
"index": "plain-text",
"name": "Plain Text"
},
{
"index": "text-icon",
"name": "Text Icon"
}
]
}
],
"order": "2"
}
]
var app = angular.module('mymod', []);
app.filter('createarray', function () {
return function (value, propertyName) {
var arrayList = [];
angular.forEach(value, function (val) {
angular.forEach(val[propertyName], function (v) {
arrayList.push(v)
});
});
console.log(arrayList)
return arrayList;
}
});
app.directive('everything', function() {
return {
restrict: 'E',
templateUrl: 'everything.html',
controller: function($scope, $http) {
$http.get('assets/js/test.json')
.then(function(result) {
$scope.everything = result.data;
});
},
controllerAs: 'everything'
}
});
<nav class="om-nav-everything">
<ul>
<li ng-repeat="pattern in everything.indexes | createarray: 'patterns'"><a href="index-{{pattern.index}}.html">{{pattern.name}}</a></li>
</ul>
</nav>
我的HTML看起来像:
[
{
"category": "colors",
"heading": "Colors & Background",
"indexes": [
{
"index": "colors",
"name": "Colors",
"patterns": [
{
"index": "background-patterns",
"name": "Background Patterns"
}
]
}
],
"order": "1"
},
{
"category": "typography",
"heading": "Typography",
"indexes": [
{
"index": "typography",
"name": "Typography",
"patterns": [
{
"index": "headings",
"name": "Headings"
},
{
"index": "plain-text",
"name": "Plain Text"
},
{
"index": "text-icon",
"name": "Text Icon"
}
]
}
],
"order": "2"
}
]
var app = angular.module('mymod', []);
app.filter('createarray', function () {
return function (value, propertyName) {
var arrayList = [];
angular.forEach(value, function (val) {
angular.forEach(val[propertyName], function (v) {
arrayList.push(v)
});
});
console.log(arrayList)
return arrayList;
}
});
app.directive('everything', function() {
return {
restrict: 'E',
templateUrl: 'everything.html',
controller: function($scope, $http) {
$http.get('assets/js/test.json')
.then(function(result) {
$scope.everything = result.data;
});
},
controllerAs: 'everything'
}
});
<nav class="om-nav-everything">
<ul>
<li ng-repeat="pattern in everything.indexes | createarray: 'patterns'"><a href="index-{{pattern.index}}.html">{{pattern.name}}</a></li>
</ul>
</nav>
我要做的是为每个模式都有一个列表项,这样就有了一个JSON中所有3级项目的列表
如何更改过滤器以实现此目的
这是一个带有相关代码的Plunker:所以我所做的是制作一个可以放入过滤器的函数,因为如果我做了所有的工作,有什么乐趣?这将获取一个属性名数组,当它到达最后一个属性名时,它将返回该值。这里有一个plunker来演示
var lastArray=function(arr、propertyNames、index){
var-res=[];
如果(!Array.isArray(arr)){
返回res;
}
对于(变量i=0;i
对于一点方向,我可能会尝试以下方法:
然后在createarray
过滤器中,拆分“,”上的字符串,并将其发送到lastArray()
编辑:我创建了一个plunker,用angular来演示它 你错过了Plunker中的
everything.html
文件。太棒了,我已经完成了80%。我有一些例子,其中“索引”有2个或更多的元素(因此不仅仅是颜色,还有颜色和阴影,例如,每个元素都有自己的模式)。您的解决方案允许我抓取“索引”下的第一个元素,是否可以对其进行增强以抓取“索引”下可能存在的所有元素及其模式?可能是这样,但现在我们进入了一个特定的用例。这里我将索引
对象放入结果数组。但是,问题是,由于属性名称可能不同,如果没有索引['name']
,则属性名称将为空。此外,这将添加每个级别。因此,如果你深入n层,它将继续添加父级。但希望这是一个起点,您可以对其进行增强。谢谢,我在这里更新的实际上是JSON:我可以尝试更改您显示的内容,因为它会跳过任何包含多个“索引”对象的“索引”。例如,在Plunker中,将跳过Shades对象。绝对不会要求你更新,因为你已经帮了很大的忙(而且你可以忽略我!),但我想澄清一下。