首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS动态访问JSON索引值

AngularJS动态访问JSON索引值
EN

Stack Overflow用户
提问于 2015-06-03 13:07:06
回答 2查看 1.4K关注 0票数 1

我的范围数据:

代码语言:javascript
复制
$scope.data = 
 "category": [{
"name": "cat1",
"behaviour": "normal",
"selected": 0,
"values": [{
    "label": "define",
    "count": 6
}]
}, {
"name": "cat2",
"behaviour": "normal",
"selected": 0,
"values": [{
    "label": "type",
    "count": 6
}]
}, {
"name": "Company",
"behaviour": "multi-select",
"selected": 0,
"values": [{
    "label": "VW",
    "count": 4
}, {
    "label": "Renault",
    "count": 1
}, {
    "label": "Fiat",
    "count": 1
}]
}, {
"name": "Make",
"behaviour": "multi-select",
"selected": 0,
"values": [{
    "label": "Gold",
    "count": 3
}]
}, {
"name": "Color",
"behaviour": "normal",
"selected": 0,
"values": [{
    "label": "White",
    "count": 3
}, {
    "label": "Blue",
    "count": 2
}, {
    "label": "Green",
    "count": 1
}]
}]

不使用索引,如何访问"name":"value“?随着数据的增长和变化,我不想在任何地方分配索引值。我仍然想要过滤,例如:

代码语言:javascript
复制
| {name: 'Make'}: true)

在我的标记上显示出来

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-03 13:17:45

所以json是不正确的,我更正了它(下次您可以使用这个站点查看您的json是否有效的JSONVALIDATOR),如果您想访问名称的值,请这样做。

在您的控制器中:

代码语言:javascript
复制
$scope.data = [
{
    "name": "cat1",
    "behaviour": "normal",
    "selected": 0,
    "values": [
        {
            "label": "define",
            "count": 6
        }
    ]
},
{
    "name": "cat2",
    "behaviour": "normal",
    "selected": 0,
    "values": [
        {
            "label": "type",
            "count": 6
        }
    ]
},
{
    "name": "Company",
    "behaviour": "multi-select",
    "selected": 0,
    "values": [
        {
            "label": "VW",
            "count": 4
        },
        {
            "label": "Renault",
            "count": 1
        },
        {
            "label": "Fiat",
            "count": 1
        }
    ]
},
{
    "name": "Make",
    "behaviour": "multi-select",
    "selected": 0,
    "values": [
        {
            "label": "Gold",
            "count": 3
        }
    ]
},
{
    "name": "Color",
    "behaviour": "normal",
    "selected": 0,
    "values": [
        {
            "label": "White",
            "count": 3
        },
        {
            "label": "Blue",
            "count": 2
        },
        {
            "label": "Green",
            "count": 1
        }
    ]
}
];

在HTML中

代码语言:javascript
复制
      <div ng-repeat="d in data"> {{d.name}} </div>

如果您想用名称“Company”显示对象的值,可以在我的工作CodePen中这样做:

只需添加一个新的ng-重复:

代码语言:javascript
复制
   <div ng-repeat="d in data"> {{d.name}} 
         <div ng-if="d.name == 'Company'" ng-repeat="da in d.values">{{da.label}}</div>      
  </div>
票数 1
EN

Stack Overflow用户

发布于 2015-06-03 13:18:25

首先,数据的格式是不正确的JSON格式,通过在线验证器运行它。

要访问元素,只需通过:

代码语言:javascript
复制
<li ng-repeat="item in data">
    {{ item.name }} {{ item.behaviour }} etc...
</li>

去掉$scope.data中的类别.让它成为一个数组,就像一个典型的JSON值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30621018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档