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

2
  • use $scope or angular model {{model}} Commented Apr 10, 2017 at 8:21
  • Can you please share the code for this Commented Apr 10, 2017 at 8:28

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

9 Comments

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
<li ng-repeat="(key, value) in inputs" > <div ng-repeat="(key1, value1) in value"> <label>{{key1}}</label> <input type="text" name="" value="{{value1}}">
@DeepakVijay do you mean the object contains inner object?
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
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
|
0

/My html should look like this/

  <head>
    <script data-require="[email protected]" 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>

1 Comment

you should add details to your question, not by answering here.
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>

4 Comments

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)
@DeepakVijay I updated the answer. you can check now.
:- 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..
This small thing you can do yourself also. I can not give everything on your plate.
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);

2 Comments

@Pengyy : please have a look at it and give me suggestions
@Rohit : please have a look at it this is the way i wanted

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.