将数据推送到嵌套数组,然后使用http post方法将整个数据发送到API

时间:2016-06-21 08:17:40

标签: javascript html arrays angularjs json

这是我的json文件,我想将数据推送到workingDay(多数据推送)& workingHours位于数组workSchedules内。

    {
  "data": {
    "id": "string",
    "workingSchedules": [
      {
        "workingDay": "MONDAY",
        "workingHours": [
          {
            "fromTime": 0,
            "toTime": 0
          }
        ]
      }
    ],
    "twentyFourSevenOpen": false
  },
  "dataList": [
    {
      "id": "string",
      "workingSchedules": [
        {
          "workingDay": "MONDAY",
          "workingHours": [
            {
              "fromTime": 0,
              "toTime": 0
            }
          ]
        }
      ],
      "twentyFourSevenOpen": false
    }
  ]
}

我的代码就像这样

<table class="table table-bordered table-hover">
          <tr>
            <th class="col-md-4">Day</th>
            <th class="col-md-4">From time</th>
            <th class="col-md-4">To time</th>
        </tr>
        <tbody>

       <form class="form-horizontal" role="form" ng-repeat="x in something.workingSchedules">
              <div class="form-group">

                 <div class="col-sm-4">  
                     <input style="margin-top:15px" ng-model="x.workingDay" class="form-control input-sm" id="pwd" placeholder="Timings">
                 </div>
              </div>
          <form class="form-horizontal" role="form" ng-repeat="x in x.workingHours">

              <div class="form-group">
                 <div class="col-sm-4" >  
                     <input ng-model="x.fromTime" class="form-control input-sm" id="pwd" placeholder="Timings">
                 </div>
              </div>

              <div class="form-group">
                 <div class="col-sm-4">  
                     <input ng-model="x.toTime" class="form-control input-sm" id="pwd" placeholder="Timings">
                 </div>
              </div>

           </form>
         </form>

   </tbody>

这只允许更新单个workingSchedules对象,我想要的是先在UI中将数据推送到数组中,然后使用http post将其发布到后端API。

1 个答案:

答案 0 :(得分:0)

首先,workingDays需要是一个数组,与workingHours相同。

完成后,您可以使用以下内容:

data.workingSchedules.workingDay.push('FRIDAY') ;

一般来说,我建议数组中的对象始终采用以下形式:

{"DayName":"MONDAY"}

而不仅仅是当天的名字。你当前的表格会有效。