0

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.

8
  • What does "it really didn't work" mean?. Commented Dec 7, 2015 at 16:37
  • Also it would be useful to get a plnkr showing your issue. Commented Dec 7, 2015 at 16:38
  • it doesn't clear the ng-* classes to reset the form to its original state with no validation performed Commented Dec 7, 2015 at 16:40
  • You tried $scope.create.$setUntouched(); - depending on the version you're using. Commented Dec 7, 2015 at 16:45
  • @Darren you're a genius that's exactly what I wanted. I guess I messed that functionality to what $setPristine() does. Post as answer and I will accept it :) Commented Dec 7, 2015 at 16:46

2 Answers 2

1

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: ""
  };
}
Sign up to request clarification or add additional context in comments.

Comments

0

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= '';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.