簡體   English   中英

如何在Angularjs中創建嵌套的json對象

[英]How to create nested json object in Angularjs

我想在angularjs中創建嵌套的json對象。 我的目標是這樣的:

   {
    "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
      }
     ]
   }
 ]
}

為了動態創建此對象,我編寫此代碼。已經初始化了訂單和項目,創建了表單。 例如,項目和訂單的大小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>

你可以這樣做:

$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