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>
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With