I can not understand how to add objects to the array. That is, you need to add a separate category and the ability to add category parameters. Because I do not quite understand how multidimensional arrays work. I ask for help from experts, to solve this problem. Below is an example on the planer.

HTML

<body ng-controller="DataBaseController"> <input type="text" ng-model="addNameCat" placeholder="Название новой категории"> <button ng-click="addCatItem()" type="submit">+ Категорию</button> <div ng-repeat="cat in database"> <p>{{cat.category}} <button ng-click="removeCatItem($index)">x</button> <input type="text" ng-model="addNameParam" placeholder="Новый параметр"> <button ng-click="addCatItem()" type="submit">+ Параметр</button> </p> <ul ng-repeat="datafields in cat.datafields"> <li>{{datafields.param}} <button ng-click="removeParamItem($parent.$index, $index)">x</button> </li> </ul> </div> </body> 

Json

 [{ "category": "Категория 1", "datafields": [{ "param": "Категория1 - Текст1" }] }, { "category": "Категория 2", "datafields": [{ "param": "Категория2 - Текст1" }, { "param": "Категория2 - Текст2" }] }] 

Js

 angular .module('app', []) .controller('DataBaseController', function($scope, $http) { $http.get('data.json').success(function(data, status, headers, config) { $scope.database = data; $scope.addNameCat = ''; $scope.addNameParam = ''; $scope.addCatItem = function() { if ($scope.addNameCat !== '') { // ??? Добавляем категорию $scope.addNameCat = ''; } }; $scope.addParamItem = function() { if ($scope.addNameParam !== '') { // ??? Добавляем параметр категории $scope.addNameParam = ''; } } $scope.removeCatItem = function(indexCat) { data.splice(indexCat, 1); }; $scope.removeParamItem = function(indexCat, indexParam) { data[indexCat].datafields.splice(indexParam, 1); }; }); }); 

Planker example

  • one
    Multidimensional arrays are not visible here. Objects are added to the array as usual in js (using push). Here is a revised example of yours: plnkr.co/edit/ayCntw - Vladimir Gamalyan
  • Everything is much easier than I thought, thanks for the help! - sashatexb

0