Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

i want to create nested json object in angularjs. my object is this:

   {
    "marketerId": 1,
     "baskets": [
      {
        "customer": {
           "phone": ""
         },
        "region": 1,
        "orders": [
         {
           "bookId": 1,
           "count": 5
         },
         {
           "bookId": 2,
           "count": 52
         }
      ]
   },
    {
      "customer": {
          "phone": ""
     },
      "region": 1,
    "orders": [
     {
        "bookId": 1,
        "count": 12
     },
     {
        "bookId": 2,
        "count": 2
      }
     ]
   }
 ]
}

For create this object as dynamically i write this code.Assuming orders and items already have been initialized, the form is created. For example, the size of the items and orders 2.Is there a better way to build nested json objects?

     <input ng-model="formData.marketerId" />
    <div class="row" ng-repeat="item in items track by $index">
        <input ng-model="formData.baskets[$index].customer.phone" />
        <input ng-model="formData.baskets[$index].region" />
        <div  ng-repeat="order in orders track by $index">
           <input type="text" ng-model=
            "formData.baskets[$parent.$index].orders[$index].bookId">
             <input type="text" ng-model=
            "formData.baskets[$parent.$index].orders[$index].count">

        </div>
    </div>
share|improve this question
1  
create an object with just the keys, assign it to the models and on submit push it to your main array. Done – Suman Lama Aug 6 '15 at 5:19

You can do something like this:

$scope.data1 = [];
var firstObj = new Object();
firstObj.first = "value1";
firstObj.second = "value2";
$scope.encountersData.push(firstObj);

$scope.data2 = [];
var  secondObj= new Object();
secondObj.third = "value3";
secondObj.fourth = "value4";
$scope.data2.push(secondObj);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.