0
 var inputs={
    'firstname': '',
    'lastName':'',
   'account':{
     'role':'',
     'status':''
   }
 }

This is my model array. I want to display it dynamically in Webpage and by modifying the json array the changes should affect the form too.

Here is the image enter image description here

Pengyy
  • 37,383
  • 15
  • 83
  • 73
Deepak Pookkote
  • 957
  • 3
  • 14
  • 32

4 Answers4

1

UPD: for your situation, you can use ng-switch to generate elements according to conditions.


Notice(already included in the code snippet): ng-repeat will generate it's own scope, so your model won't update unless you bind it with the original scope. ref here.


OLD ANSWER: use ng-model to implement two-way-databinding. refer the code snippet below:

angular.module("app", []).controller("myCtrl", function($scope) {
  $scope.inputs = {
    'firstname': 'test first name',
    'lastName': 'test last name',
    'account': {
      'role': 'test role',
      'status': 'test status'
    }
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  <!-- First Name: <input type="text" ng-model="inputs.firstname"><br>     
       Last Name: <input type="text" ng-model="inputs.lastName"><br>        Account Role: <input type="text" ng-model="inputs.account.role"><br> 
       Account Status: <input type="text" ng-model="inputs.account.status"><br> -->
  <div ng-repeat="(key1, value) in inputs" ng-switch="key1">
    <div ng-switch-when="account">
      <div ng-repeat="(key2, value2) in value">
        {{key1 | uppercase}} => {{ key2 | uppercase}}
        <input type="text" ng-model="inputs[key1][key2]">
      </div>
    </div>
    <div ng-switch-default>
      {{key1 | uppercase}}
      <input type="text" ng-model="inputs[key1]">
    </div>
  </div>
  {{inputs}}
</div>
Community
  • 1
  • 1
Pengyy
  • 37,383
  • 15
  • 83
  • 73
  • it should not be like this ...see the array contains inner arrays and i cannot use the input fields here it should be created automatically from the json array model – Deepak Pookkote Apr 10 '17 at 08:32
  • @DeepakVijay do you mean the `object` contains inner `object`? – Pengyy Apr 10 '17 at 08:34
  • yes the object contains inner object and its a nested json array..it should check each time array values.. and if it is an inner array it should print it..and we should not use the code each time to create input fields – Deepak Pookkote Apr 10 '17 at 08:35
  • yes the object contains inner object and its a nested json array..it should check each time array values.. and if it is an inner array it should print it..and we should not use the code each time to create input fields – Deepak Pookkote Apr 10 '17 at 08:48
  • @DeepakVijay Please update your question and post the nested json array which you are using. Also add a fiddle or plunkr if possible – Nishant123 Apr 10 '17 at 09:14
  • @Pengyy :- this code only works when i use this json array ..what if i have some dynamic array ? in which fields can be modified all the time (Add or Remove)..i need the solution for that – Deepak Pookkote Apr 10 '17 at 10:19
  • @Nishant123 :- i did posted the nested array format that i'm using ..i need my form fields should dynamically made from the json array – Deepak Pookkote Apr 10 '17 at 10:21
  • @DeepakVijay I think this have answered specifically what you have asked. Please mark this as an answer and make a new question if you have other problem and I will be glad to offer some help there. – Pengyy Apr 10 '17 at 10:29
0

/My html should look like this/

  <head>
    <script data-require="angular.js@1.4.1" data-semver="1.4.1" src="https://code.angularjs.org/1.4.1/angular.js"></script>
    <script src="script.js"></script>
  </head>

  <body ng-controller="MainCtrl">


    <script type="text/ng-template" id="tree-structure">
    <label>{{dt}}</label><input type="text" name="" value="{{dt.label}}">
     <ul class="childElement">
        <li ng-repeat="dt in dt.nodes" ng-include="'tree-structure'">
        </li>
     </ul>
</script>

<ul class="parentList">
    <li ng-repeat="(key, value) in inputs" >
        <div  ng-repeat="(key1, value1) in value">
            <label>{{key1}}</label>
            <input type="text" name="" value="{{value1}}">

           <!--  <div ng-repeat="(key2, value2) in value1">
              <label>{{key2}}</label><input type="text" name="" value="{{value2}}"> 
            </div> -->

        </div>
        <div ></div>
    </li>

    </div>
</ul>
  </body>

</html>
Deepak Pookkote
  • 957
  • 3
  • 14
  • 32
0

Some observations :

  • Your JSON should be formatted properly with type of the field.
  • If you want to access the object properties as a form fields then it should be structured in a good way so that we can dynamically add the type of the field as well.

    [{
        name: 'firstName',
        type: 'text'
    }, {
        name: 'lastname',
        type: 'text'
    }, {
        account: [{
            name: 'role',
            type: 'text'
        }, {
            name: 'status',
            type: 'text'
        }]
    }]
    
  • As your JSON have nested objects. So, first iterate it recursively and create one dimensional array then create the fields using 1D array.

DEMO

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    var inputs = [{
 name: 'firstName',
 type: 'text'
}, {
 name: 'lastname',
 type: 'text'
}, {
 account: [{
  name: 'role',
  type: 'text'
 }, {
  name: 'status',
  type: 'text'
 }]
}];

$scope.fields = [];
function structuredObj(obj) {
  for (var i in obj) {
     if (obj[i].type == 'text') {
       $scope.fields.push(obj[i]);
     } else {
       structuredObj(obj[i])
     }
  }
};

structuredObj(inputs);

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<form name="myForm" novalidate>
  <div ng-repeat="item in fields" class="form-group">
        <input
            name="item.name"
            type="{{ item.type }}"
            placeholder="{{ item.name }}"
            ng-model="item.value"
            required />
  </div>
<button ng-disabled="myForm.$invalid">Submit</button>
</form>
</div>
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • you used 2 ng-repeats ..this will work upto 2-dimension ..what if in the case of 3D and 4d and more than that..i need solution for that..i want solution for any number of fields that can be made dynamically from any json array (nested Json) – Deepak Pookkote Apr 10 '17 at 10:31
  • @DeepakVijay I updated the answer. you can check now. – Debug Diva Apr 10 '17 at 11:04
  • :- please provide the answer in the format please . i need labels to be added and sub array should be shown as sub array with arrows.. – Deepak Pookkote Apr 10 '17 at 11:08
  • This small thing you can do yourself also. I can not give everything on your plate. – Debug Diva Apr 10 '17 at 11:13
0
<div ng-repeat="(key1, value1) in myPersonObj">
    <div ng-repeat="(key, value) in value1">
        <label>{{key}}</label>
        <input type="text" name="" value="{{value}}">
    </div>
</div>

var app = angular.module("test",[]);

app.controller("MainCtrl",function($scope){

  $scope.inputs = [
    {
      "firstname" : "Test"
    },{
      "lastname" : "Test1"
    },{                           
      "Account" : [

        {"role" : "Test3"},
        {"status" : "Test4"},
      ]
    },
    {
      "Account1" : [

        {"role" : "Test3"},
        {"status" : "Test4"},
      ]
    },
    {
      "Account2" : [

        {"role" : {
          'dim3': {
            'dim4':{
              'dim5':'cccc'
            }
          }
        }

      },
        {"status" : "Test4"},
      ]
    }
  ];
$scope.person = [];
$scope.myPersonObj = [];
/*console.log($scope.keys(inputs));*/
    $scope.checkIndex1 = function(arg, myPersonObj)
    {
        if (angular.isArray(arg) || angular.isObject(arg)) {
            angular.forEach(arg, function (value, key) {
                console.log(value);
                if(angular.isObject(value) || angular.isArray(value))
                {
                    $scope.checkIndex1(value, myPersonObj);
                }
                else
                {
                    console.log("pushing");
                    myPersonObj.push(arg);
                }
            });
        }
        else
        {
            console.log("pushing1");
            myPersonObj.push(arg);
        }      
    }

    $scope.checkIndex1($scope.inputs, $scope.myPersonObj);

    console.log("myPersonObj :"+ JSON.stringify($scope.myPersonObj));

    console.log($scope.inputs);
Deepak Pookkote
  • 957
  • 3
  • 14
  • 32