首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JS/JSON -通过JS访问JSON中的多个数组

JS/JSON -通过JS访问JSON中的多个数组
EN

Stack Overflow用户
提问于 2015-03-25 18:46:07
回答 1查看 94关注 0票数 0

我想稍微清理一下我的AngularJS代码。现在,我的JSON数组在$scope.array下的JS文件中,有5个组。我是否可以将它们都放在一个单独的JSON文件中,或者每个数组需要多个文件?如果我把它们放在一个JSON文件中,我仍然可以通过$http.get()访问它们吗?

下面是带有JSON数据的JS代码:

代码语言:javascript
运行
复制
var reportApp = angular.module('reportApp', ['ui.bootstrap']);

reportApp.controller('reportController', function($scope) {
    $scope.head = [
                   {id: 1, name: 'Application Name', class: 'filtersearch'},
                   {id: 2, name: 'Date Created'},
                   {id: 3, name: 'Date Updated'},
                   {id: 4, name: 'Payload'},
                   {id: 5, name: 'Status', class: 'filtersearch'},
                   {id: 6, name: 'Error Code', class: 'filtersearch'},
                   {id: 7, name: 'Error Description'}
                   ];

    $scope.details = [
                      {id: 1, name: 'Application Name'},
                      {id: 2, name: 'Error Description'},
                      {id: 3, name: 'Record Count'},
                      {id: 4, name: 'Record Fail'}
                      ];

    $scope.status = [
                     {id: 1, name: 'Active'},
                     {id: 2, name: 'Inactive'},
                     {id: 3, name: 'Unknown'}
                     ];

    $scope.errorCode = [
                        {id: 1, name: 'Code01'},
                        {id: 2, name: 'Code02'},
                        {id: 3, name: 'Code03'},
                        {id: 4, name: 'Code04'}
                        ];

    $scope.apps = [
                   {appName: 'App01',
                       dateCreated: '01/01/2015',
                       dateUpdated: '01/04/2015',
                       payload: 'Payload01',
                       status: $scope.status[0],
                       errorCode: $scope.errorCode[0],
                       errorDesc: 'Desc01',
                       recordCount: 1,
                       recordFail: 1},
                   {appName: 'App01',
                       dateCreated: '01/02/2015',
                       dateUpdated: '01/05/2015',
                       payload: 'Payload02',
                       status: $scope.status[0],
                       errorCode: $scope.errorCode[1],
                       errorDesc: 'Desc02',
                       recordCount: 1,
                       recordFail: 2},
                   {appName: 'App03',
                       dateCreated: '01/03/2015',
                       dateUpdated: '01/06/2015',
                       payload: 'Payload03',
                       status: $scope.status[1],
                       errorCode: $scope.errorCode[2],
                       errorDesc: 'Desc03',
                       recordCount: 2,
                       recordFail: 1}
                  ];
});

我希望以类似于以下代码的方式使用$http.get(),但适用于多个数组:

代码语言:javascript
运行
复制
var report = this;
report.apps = [];
$http.get('apps.json').success(function(data){
    report.apps = data;
});

任何输入都非常感谢!

编辑:这是JS Fiddle

代码语言:javascript
运行
复制
var report = this;
report.apps = [];
$http.get('../controllers/apps.json').success(function(data){
    for (head in data) {
        report.head = data.head;
    }
    for (details in data) {
        report.details = data.details;
    }
    for (status in data) {
        report.status = data.status;
    }
    for (errorCode in data) {
        report.errorCode = data.errorCode;
    }
    for (apps in data) {
        report.apps = data.apps;
    }
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-25 18:53:29

你可以把它们放在一个单一的对象下:

代码语言:javascript
运行
复制
{ head : [
               {id: 1, name: 'Application Name', class: 'filtersearch'},
               {id: 2, name: 'Date Created'},
               {id: 3, name: 'Date Updated'},
               {id: 4, name: 'Payload'},
               {id: 5, name: 'Status', class: 'filtersearch'},
               {id: 6, name: 'Error Code', class: 'filtersearch'},
               {id: 7, name: 'Error Description'}
               ],
 details : [
                  {id: 1, name: 'Application Name'},
                  {id: 2, name: 'Error Description'},
                  {id: 3, name: 'Record Count'},
                  {id: 4, name: 'Record Fail'}
                  ]
}

然后迭代该对象,并分配到存储:

代码语言:javascript
运行
复制
$http.get('../store-products.json').success(function(data){
    for (key in data) {
        store[key] = data[key];
    }
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29263996

复制
相关文章

相似问题

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