使用Angular js提交后重置表单

时间:2018-06-27 08:29:00

标签: javascript jquery angularjs forms reset

我想在提交表单后重置表单字段,但我的代码运行正常,但是在这里清除输入的字段是这样编写的:所有复选框字段,我必须编写的所有复选框字段。

$scope.appointmentForm = function() {
$scope.formData = {};
$http({
    method: "POST",
    url: "appointment_mail.php",
    data: {
        date: $scope.appt_date,
        fname: $scope.fname,
        lname: $scope.lname,
        email: $scope.email,
        notes: $scope.notes,
        services: $scope.services,
        category: $scope.category,
        address: $scope.address,
        timings: $scope.appt_timings,
        phone: $scope.phone,
        services: $scope.services,
        category: $scope.category
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(function(response) {
    data = response.data;
    if (!data.success) {
        // if not successful, bind errors to error variables
        $scope.errorFname = data.errors.fname;
        $scope.errorLname = data.errors.lname;
        $scope.errorMail = data.errors.email;
        $scope.errorPhone = data.errors.phone;
        $scope.errorNotes = data.errors.notes;
        $scope.errorAddress = data.errors.address;
        $scope.messages = null;
    } else {
        // if successful, bind success message to message
        $scope.fname = '';
        $scope.lname = '';
        $scope.email = '';
        $scope.note = '';
        $scope.address = '';
        $scope.phone = '';
        $scope.appointment_form.$setPristine();
        $scope.messages = data.messages;
        $timeout(function() {
            // Loadind done here - Show message for 3 more seconds.
            $timeout(function() {
                $scope.messages = false;
            }, 3000);
        }, 2000);
        //From here is there any other alternative way
        $scope.category = {
            "red": "",
            "blue": "",
            "green": "",
        }
        $scope.services = {
            "abc": "",
            "xyz": "",
        }
        $scope.appt_timings = "";
        $scope.notes = "";
        $("#iva_apptdate").datepicker('setDate', null);
    }
}, function(error) {});
}
});
<div class="check-wrap">
  <div class="cat">
    <span class="checkbox">
      <input type="checkbox" name="category" checked data-ng-model="iva_category.red">                                                               
      <label>Red
      </label>
    </span>
  </div>
  <div class="cat">
    <span class="checkbox">
      <input type="checkbox" name="category" data-ng-model="category.blue">
      <label>Blue
      </label>
    </span>
  </div>
  <div class="cat">
    <span class="checkbox">
      <input type="checkbox" name="category" data-ng-model="category.green">
      <label>Green/label>
        </span>
      </div>
  </div>

我的问题是是否有其他方法可以执行此操作,因为在这里我必须编写更多代码以清除复选框输入字段。请随时告诉我是否有任何错误。

0 个答案:

没有答案