i want to create json object in Angularjs. when add on addBasket add new basket object to array and when click on addOrder add new order to array. in fact date field not repeat in loop. dothis way is good to create json object? or may be exist good solution to create json object.
Edit question:
I would like to create object in the json format. I put the format below. I wrote the code that I've put it. In fact, my problem is to add an object to the orders array. how add new object to orders array?
[ { "date":'2015-30-7', "baskets": [ { "orders": [ { "id": "12" } ], "customer": { "phone": "555555" }, "discount": "8" } ] } ]
var app = angular.module('app', []); app.controller('myController', function($scope, $http) { $scope.typistData = []; $scope.typistData.push({ baskets: [{ orders:[] }] }); $scope.addBasket = function() { $scope.typistData.push({ baskets: [{ orders:[] }] }); }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app='app' ng-controller='myController'> <input type="text" ng-model="data.name"> <hr> <div ng-repeat="data in typistData" ng-if="typistData"> <form novalidate ng-submit="submitOffices()"> <div> <ng-form ng-repeat="basket in data.baskets"> <div> <input type="text" ng-model="basket.customer.phone"> <br> <input type="text" ng-model="basket.discount"> <input type="text" ng-model="basket.orders[0].id"> <input type="button" value="addOrder"> </div> <hr> </ng-form> </div> </form> </div> <button ng-click="addBasket()">Add Basket</button> <div> <pre>{{ typistData | json }}</pre> </div> </div>