简体   繁体   English

如何在Angularjs中创建嵌套的json对象

[英]How to create nested json object in Angularjs

i want to create nested json object in angularjs. 我想在angularjs中创建嵌套的json对象。 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? 例如,项目和订单的大小2.是否有更好的方法来构建嵌套的json对象?

     <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>

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM