22

I have an AngularJS form that contains - among other fields - one of type url. The latter is important as this forces the corresponding input to be a valid URL.

Under certain conditions (for instance, a modal dialog with such a form is to be closed), I want to clear that form programmatically. For that purpose, I implemented method reset that basically clears the corresponding form model by setting $scope.formData = {}. Thus, it sets the form model to a new, blank object.

While that assignment clears all valid fields in the rendered HTML form, it does not clear invalid fields, like an invalid URL. For instance, if the user would provide invalid input ht://t/p as URL, that input would not be removed from the rendered form.

I think this is due to the fact that any invalid URL is not reflected by the model - such an invalid URL just wouldn't "make" it to the model because it does not pass validation in the NgModelController#$parsers array. Thus, in the model - there is no URL at all. Consequently, resetting the form model to {} cannot actually change the model's URL as it has not been set yet.

However, if method reset explicitly sets field $scope.formData.url = "", the invalid URL will be cleared properly (at least, the rendered form won't show it anymore). This is caused by the explicit change of the URL in the model. However, now, model variable formData.url contains the empty string (well, not surprisingly), while by using = {}, all fields would be undefined instead.

While assigning individual fields to "" works as workaround for simple forms, it quickly becomes cumbersome for more complex forms with many fields.

Thus, how could I programmatically reset the form efficiently and effectively - including all invalid input fields as well?

I created a Plunker at http://plnkr.co/c2Yhzs where you can examine and run a complete example showing the above effect.

4
  • The idea of "clean" form means after invoke some action - to return form to initial state (aka like after page reload). In your case $scope.formData = {} Commented Dec 9, 2013 at 19:16
  • @MaximShoustin I would like to reset both the model and the view. With $scope.formData = {}, only the model is reset. Commented Dec 10, 2013 at 9:36
  • Any progress on this? Just bit me too. Commented Feb 11, 2014 at 14:46
  • Any news about how to solve this? I am facing also the same problem with the invalid fields. Commented Oct 1, 2014 at 0:38

7 Answers 7

5

Specify the type of your button as reset. That will not only call the ngClick function, it will also clear the content of the HTML form.

<button type="reset" ng-click="resetFormData()">Reset</button>
Sign up to request clarification or add additional context in comments.

1 Comment

This works great for resetting the form completely. Unfortunately, in my case I want to reset the form (and model) to an initial state, e.g. my checkbox should be initially checked. Setting the model-value to true in the ngClick-handler is not respected by the view if I click the <button type="reset">
2

I think this solution is moderately elegant: your plnkr reviewed
The big difference is the initialization of your model object.

I think things gets messed up when a variable becomes undefined, it doesn't get updated anymore.. it should be connected (veeeery) deeply with how validation works (docs link)

Returning undefined in that case makes the model not get updated, i think this is exactly what happens behind the curtain

PS: you can recycle resetImplicitly for all your forms in the webapp :)

2 Comments

You saved my day!!! I used the explicit mode, forcing to be an empty string: $scope.filter.code = ""
I don't believe this method works w/ version 1.8.2. I changed the plnkr to that version and it no longer worked.
1

After trying several answers without success in similar questions, this worked for me.

In my controller:

$scope.cleanForm = function() {
  $scope.myFormName.$rollbackViewValue();
};

Just call with some ng-click or any way you want.

Cheers

Comments

0

The Thing is tag is of type "url" which means if user will enter specifically a valid url then only it will set values of model

If user will expicitly reset it which means setting model values to "" will again make textbox empty .

It is looking like it is setting the values but actually not ,so when you set its value to "" .Angular will set modal value to "" Lets take another example : put replace "text" with "email"

<input type="email" ng-model="formData.name" />
      <br />URL:
      <input type="url" ng-model="formData.url" />
      <br />

In above code If you will enter invalid email it will not set the values of email's model.

Comments

0

You probably need to make a copy of the model in its pristine state and set the model to pristine when you reset.

There's a good example here:

http://www.angularjshub.com/examples/forms/formreset/

Comments

0

The url form fields are passed into the model only if they are valid. Thus in case of an invlaid-url entry in the form, the scope variable is not assigned with the model and clearing the forms entry by assigning an empty object to the model will still persist the value at the UI front.

The best alternative to this is to assign the model associated with the form data with a null. A similar answer appears here:

https://stackoverflow.com/a/18874550/5065857

Comments

-2

ng-click="formData={};"

just give like this ,

<button ng-click="formData={}">(1) Reset Full Data: formData = {}</button>

Reset your form data directly in ng-click itself.

1 Comment

This isn't true. It only works for inputs which are valid. There is something like $viewValue which is different to $modelValue which you're deleting.

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.