zilcuanu
zilcuanu

Reputation: 3715

clearing angularJs form

I am using AngularJs 1.4 and having a input form. I have a reset, cancel and submit button at the end of the form. When I click, all the form elements must be set to default values. But the submitForm function is also getting called along with the resetForm function when I click the reset button. Please let me know where I am going wrong. The form fields are also not getting reset.

<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>

Inside my controller I have the below code:

 $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;
    }

Upvotes: 0

Views: 50

Answers (2)

&#193;lvaro Touz&#243;n
&#193;lvaro Touz&#243;n

Reputation: 1230

You could use $setPristine(); to the form, as the doc in angular https://docs.angularjs.org/api/ng/type/form.FormController says

"Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it."

expect help you

Upvotes: 0

Gracia
Gracia

Reputation: 214

Just change the myForm.ipaddresses=null; to $scope.ipaddresses='';.

It should look like below:

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

Upvotes: 1

Related Questions