0

我正在使用 AngularJs 1.4 并有一个输入表单。我在表单末尾有一个重置、取消和提交按钮。当我单击时,所有表单元素都必须设置为默认值。但是当我单击按钮时,该函数submitForm也会与该函数一起被调用。请让我知道我哪里出错了。表单字段也没有被重置。resetFormreset

<form class="form-horizontal" name="myForm" ng-submit="submitForm(myForm.$valid)" novalidate><!-- novalidate prevents HTML5 validation since we will be validating ourselves -->
<div class="form-group" ng-class="{ 'has-error' : myForm.ipaddresses.$invalid && !myForm.ipaddresses.$pristine }">
  <label class="col-sm-3 control-label">IP</label>
  <div class="col-md-6"><textarea name="ipaddresses" class="form-control" ng-model="ipaddresses" ng-minlength="8" ng-requied="!myForm.myFile"></textarea></div>
    <p ng-show="myForm.ipaddresses.$invalid && !myForm.ipaddresses.$pristine" class="help-block">Hostname or IP Addresses are mandatory</p>
  </div>
  <div class="hr-line-dashed"></div>
  <div class="form-group">
    <label class="col-sm-3 control-label">File Upload</label>
    <div class="col-lg-6">
      <div class="upload-file">
        <input type="file" file-model="myFile" class="upload-demo" id="upload-demo" ng-requied="!myForm.ipaddresses">
        <label for="upload-demo" data-title="Select file">
          <span class="ng-binding">Comma separated CSV file...</span>
        </label>
      </div>
    </div>
  </div>
  <div class="hr-line-dashed"></div>
  <div class="form-group"><label class="col-sm-3 control-label">Mode</label>
    <div class="col-md-3">
      <select name="mode" class="form-control m-b" ng-required="true">
        <option>Enable</option>
        <option>Disable</option>
      </select>
    </div>
  </div>
  <div class="hr-line-dashed"></div>
   <div class="form-group" style="margin-top: 8%;">
    <div class="col-sm-4 col-sm-offset-4">
      <button class="btn btn-info">Cancel</button>
      <button class="btn btn-warning" ng-click="resetForm(myForm)">Reset</button>
      <button class="btn btn-primary" type="submit" ng-disabled="myForm.$invalid">Next >></button>
    </div>
  </div>
</form>

在我的控制器内部,我有以下代码:

 $scope.submitForm = function(isValid) {
    // check to make sure the form is completely valid
    if (isValid) {
      alert('our form is amazing');
    }
};

 $scope.resetForm=function(myForm){
        console.log('came here....')
        myForm.ipaddresses=null;
    }
4

2 回答 2

1

只需更改myForm.ipaddresses=null;$scope.ipaddresses='';.

它应该如下所示:

$scope.resetForm=function(myForm){
    console.log('came here....');
    $scope.ipaddresses='';
}
于 2016-02-18T10:08:13.987 回答
0

你可以使用 $setPristine(); 到表格,如角度 https://docs.angularjs.org/api/ng/type/form.FormController中的文档所说

“当我们想在保存或重置表单后‘重用’表单时,将表单设置回原始状态通常很有用。”

期待帮助你

于 2016-02-18T10:12:21.147 回答