0

I would like to clean up my AngularJS code a bit. Right now, I have my JSON arrays in the JS file under $scope.[array] and there are 5 groups. Can I put all of them in 1 separate JSON file or do I need multiple files for each array? If I put them in one JSON file, can I still access them via $http.get()?

Here is the JS code with JSON data:

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}
                  ];
});

I was hoping to use $http.get() in a similar fashion to the below code, but for multiple arrays:

var report = this;
report.apps = [];
$http.get('apps.json').success(function(data){
    report.apps = data;
});

Any input is much appreciated!

EDIT: Here is the JS Fiddle.

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;
    }
});
1
  • 3
    Just to be pedantic, those are JavaScript arrays, not JSON arrays. JSON is a serialization format. Those are simple array literals, plain native JavaScript. Commented Mar 25, 2015 at 18:46

1 Answer 1

2

You could put them under a single object:

{ 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'}
                  ]
}

Then iterate that object, and assign to store:

$http.get('../store-products.json').success(function(data){
    for (key in data) {
        store[key] = data[key];
    }
});
Sign up to request clarification or add additional context in comments.

12 Comments

same way, just assign to scope: $scope[key] = data[key];
Is there a way to not use $scope?
its either $scope or the controller as method: stackoverflow.com/questions/21287794/….. if you dont have access to scope, return a promise and use then where you do have access. see this: docs.angularjs.org/api/ng/service/$q
I do have access to $scope so I could use that. Am I replacing key with anything specific from my data? I added my JS Fiddle (the modal doesn't work though) so you can see what I am trying to do.
My JSON data wasn't loading because I wasn't using double quotes. The data is loaded now, but some of my data uses $scope and won't load so I'll have to open a new question to resolve that. Thanks for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.