2016-10-05 33 views
0

我試圖找到正確的語法來推送一個新添加的項目後,它已http.posted。

以下語法不會給我任何錯誤,但也不會正確推送。任何想法將有助於

$scope.insertAppointmentReminderEntry = function() { 

    $scope.add_reminder_time  = this.add_reminder_time; 
    $scope.add_reminder_timeunit = this.add_reminder_timeunit; 
    $scope.add_delivery_method = this.add_delivery_method; 
    $scope.add_delivery_contact = this.add_delivery_contact; 

    var data = { 
     group_id    : $scope.groupid, 
     appointment_id   : $scope.appointmentid, 
     reminder_time   : $scope.add_reminder_time, 
     reminder_timeunit  : $scope.add_reminder_timeunit, 
     delivery_method  : $scope.add_delivery_method, 
     delivery_contact  : $scope.add_delivery_contact 
    }; 

     $http.post(urlApiAppointmentReminders + $scope.groupid, data).success(function(data, status) { 

      var newItemNo = $scope.appointmentreminders.length+1; 

      $scope.appointmentreminders.push($scope.appointmentreminders[newItemNo].data); 
    }) 

    modalInstance.close(); 
}; 

HTML

<div ng-repeat="x in appointmentreminders" class="row" ng-style="{ 'border-top' : (x.delivery_method != appointmentreminders[$index-1].delivery_method && $index != 0 ? '1pt solid #eee' : '0pt solid #eee')}" style="border-top:1pt solid #eee; padding: 0.25em 0 0.25em 0" > 

    <div class="col-md-offset-1 col-md-4"> 
       @{{x.time}} @{{x.timeunit}}@{{x.time > 1 ? 's' : ''}} prior to appointment 
    </div> 

    <div class="col-md-4"> 
       Reminder: @{{x.delivery_contact}} 
    </div> 

    <div class="col-md-2">     
       <!-- delete this reminder --> 
       <a class="btn btn-xs btn-warning pull-right glyphicon glyphicon-trash" ng-click="ctrl.removeItem($index)"></a>   
    </div> 

    </div> <!-- ng-repeat --> 

HTML(模態添加新的提醒)

<script type="text/ng-template" id="addScheduleModalContent.html"> 
    <div class="modal-header"> 
     <h3 class="modal-title">Add a new appointment reminder</h3> 
    </div> 
    <div class="modal-body"> 

     <div class="row" style="padding: 0.5em 0 0.5em 0.5em"> 
       <div class="col-sm-4" > 
        {!! Form::text('reminder_time', '30', ['class' => 'form-control', 'placeholder' => '', 'ng-model' => 'add_reminder_time']) !!} 
       </div> 
       <div class="col-sm-8" > 
        {!! Form::select('reminder_timeunit', ['minute' => 'minutes(s)', 'hour' => 'hour(s)', 'day' => 'day(s)', 'week' => 'week(s)', 'month' => 'month(s)'], null, ['class' => 'form-control', 'ng-model' => 'add_reminder_timeunit']) !!} 
       </div> 
     </div> 

     <div class="row" style="padding: 0.5em 0 0.5em 0.5em"> 
       <div class="col-sm-4" > 
        {!! Form::select('delivery_method', ['eml' => 'Email'], null, ['class' => 'form-control', 'ng-model' => 'add_delivery_method']) !!} 
       </div> 
       <div class="col-sm-8" > 
        {!! Form::text('delivery_contact', null, ['class' => 'form-control', 'placeholder' => 'Your email address', 'ng-model' => 'add_delivery_contact']) !!} 
       </div> 
     </div> 

    <div class="modal-footer"> 
     <button class="btn btn-primary" ng-click="insertAppointmentReminderEntry()">Save</button> 
     <button class="btn btn-primary" ng-click="$close()">Close</button> 
    </div> 
    </script>   
+1

保存自己在'NG-model'和傳球使用單個對象的大量屬性名稱重複的直接反對'$ http.post'將它們結合起來。你不想把對象推到數組中嗎? – charlietfl

+0

我相信你是對的,我有點迷失在js。例如,您的意思是將$ scope.add_reminder_time更改爲$ scope.reminder_time以匹配我的API名稱 – user3489502

+0

不在ng-model =「reminder.time」中使用一個對象'''ng-model =「reminder.contact 「'.. etc ...然後在'$ http.post'中發送'$ scope.reminder'並跳過屬性和單個變量名稱的所有手動複製 – charlietfl

回答

0

這應該是足夠$scope.appointmentreminders.push(data)

,或者如果你想要把新數據在陣列中的特定位置,然後執行此操作 $scope.appointmentreminders[newItemNo] = data

但不要在同一行:)

+0

需要確認響應中的數據是完整對象雖然因爲有2個變量'data' – charlietfl

+0

謝謝!推送工作,顯示一個新項目,但我沒有看到它的數據(除非我刷新,當然)。就像@charlietfl說的,我可能會弄亂我的財產名稱。上面添加的代碼 – user3489502

+0

剛剛注意到'data'包含'data:{「success」:true}'嗯,我該如何推送'data',其中包含我的數據。重命名它?我會嘗試 – user3489502