Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Set the model to be = {} again doesn't clear out input type='url'

I have a fiddler setup, when i click a reset button it should clear out the input controls, this seems to work but not when the input type='url'

Here is the fiddler

Is there an issue or something that I am not understanding.

When I set

$scope.myform = {};

This seems to clear out the other input type but the input type='url' isn't being cleared.

Anyone know why?

like image 412
Martin Avatar asked Sep 18 '13 13:09

Martin


People also ask

How do you clear form data after submit in AngularJS?

How do you clear form data after submit in AngularJS? 1) To Remove the values in Form Fields and to reset you can use $setPristine(); $scope. formName. $setPristine();

What does $Watch do in AngularJS?

The angular JS $watch function is used to watch the scope object. The $watch keep an eye on the variable and as the value of the variable changes the angular JS $what runs a function. This function takes two arguments one is the new value and another parameter is the old value.

How do I clear a textbox in AngularJS?

If you are using [(ngModel)] directive to control your form input fields, then you can clear it by setting an empty string ( ' ' ) to the ngModel control property.

Which property of NG-model is set to true when an input value is modified by user?

Model updates and validation By setting the allowInvalid property to true, the model will still be updated even if the value is invalid.


1 Answers

The issue you see happens when you don't have a valid value inside the input[type="url"]. An invalid value just stays in the view (the input field) and doesn't get pushed to the scope variable inside ng-model. The variable will be updated only when and if the value is correct.

You can test it by entering a valid value. The reset button will work. If you enter an invalid value it won't.

You can fix it by setting $scope.myform = null instead of $scope.myform = {}. This will empty the field because the scope variable (expression) will be undefined. It will be automatically created by Angular once you enter a valid value inside any of the fields.

like image 110
avladov Avatar answered Oct 01 '22 03:10

avladov