dabadaba
dabadaba

Reputation: 9522

AngularJS reset form completely

I have a pretty big form that's being validated on the client side by Angular. I am trying to figure out how to reset the state of the form and its inputs just clicking on a Reset button.

I have tried $setPristine() on the form but it didn't really work, meaning that it doesn't clear the ng-* classes to reset the form to its original state with no validation performed.

Here's a short version of my form:

<form id="create" name="create" ng-submit="submitCreateForm()" class="form-horizontal" novalidate>
    <div class="form-group">
        <label for="name" class="col-md-3 control-label">Name</label>
        <div class="col-md-9">
            <input required type="text" ng-model="project.name" name="name" class="form-control">
            <div ng-show="create.$submitted || create.name.$touched">
                <span class="help-block" ng-show="create.name.$error.required">Name is required</span>
            </div>  
        </div>
    </div>
    <div class="form-group">
        <label for="lastName" class="col-md-3 control-label">Last name</label>
        <div class="col-md-9">
            <input required type="text" ng-model="project.lastName" name="lastName" class="form-control">
            <div ng-show="create.$submitted || create.lastName.$touched">
                <span class="help-block" ng-show="create.lastName.$error.required">Last name is required</span>
            </div>
        </div>
    </div>
    <button type="button" class="btn btn-default" ng-click="resetProject()">Reset</button>
</form>

And my reset function:

$scope.resetProject = function() {
    $scope.project = {
        state: "finished",
        topic: "Home automation"
    };
    $("#create input[type='email']").val('');
    $("#create input[type='date']").val('');
    $scope.selectedState = $scope.project.state;
    // $scope.create.$setPristine(); // doesn't work
}

Also if you could help me clear the input values of the email and date fields without using jQuery would be great. Because setting the $scope.project to what's defined above doesn't reset the fields for some reason.

Upvotes: 0

Views: 472

Answers (2)

Darren Wainwright
Darren Wainwright

Reputation: 30727

As mentioned in the comments, you can use $setUntouched();

https://docs.angularjs.org/api/ng/type/form.FormController#$setUntouched

This should set the form back to it's new state.

So in this case $scope.create.$setUntouched(); should do the trick

Ref all that jquery. You should never interact with the DOM via controllers. That's what the directives are for

If you want to reset a given property then do something like:

$scope.resetProject = function() {
    $scope.project = {
        state: "finished",
        topic: "Home automation"
    };
    $scope.project.lastName = '';
   $scope.project.date= '';
}

Upvotes: 0

madrick
madrick

Reputation: 221

Try to clear via ng-model

$scope.resetProject = function() {
  $scope.project = {
      state: "finished",
      topic: "Home automation"
  };
  $("#create input[type='email']").val('');
  $("#create input[type='date']").val('');
  $scope.selectedState = $scope.project.state;

  $scope.project = {
      name: "",
      lastName: ""
  };
}

Upvotes: 1

Related Questions