K
K
kenjirou2016-03-28 23:13:44
JavaScript
kenjirou, 2016-03-28 23:13:44

How to add layered array to JSON from Angular controller?

I have a json file which is used as a db. It has an array with objects (single-level). Each object has an id and parentId, based on which the dependency tree should be built. I need to take data from an array and build another array in the same file, but with attachments. Those. there will be one parent object, it has several children, and these children have their own children.
There are such services:

.service('oneFactory', ['$resource', 'baseURL', function($resource, baseURL) {
    this.getOne = function() {
      return $resource(baseURL + 'firstArray');
    };
  }])

  .service('twoFactory', ['$resource', 'baseURL', function($resource, baseURL) {
    this.getTwo = function() {
      return $resource(baseURL + 'secondArray');
    };
  }]);

And controller:
.controller('BadController', ['$scope', 'oneFactory', 'twoFactory', function($scope, oneFactory, twoFactory) {
 
    $scope.firstArray = oneFactory.getOne().query(
      function(response) {
        $scope.firstArray = response;
      }
    );

    $scope.sendTwo = function() {
      for (var i = 0; i < firstArray.length; i++) {
        if (i.parentId === 1) {
          twoFactory.getTwo().save();
        }
        ...
      }
    };
  }]);

In what direction to move?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rakro, 2016-03-29
@rakro

Browser javascript cannot change the contents of the json file. Need something on the backend.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question