0

I use pattern="[0-9]{0,7}" attribute in input field, and if inputed value isn't match pattern I can't clear value with angularJs. I know about view/model in angular, and tried several ways like

$scope.model = {};

or

$scope.model = null; // $scope.model = {}; $scope.myForm.$setPristine();

here is my plnkr

Any ideas?

2
  • This is puzzling. Although it'd be nice if you put the relevant code in the question (keep the plnkr link). Commented May 15, 2017 at 16:06
  • Checkout this answer, it will clear out your concept :) Commented May 15, 2017 at 16:07

2 Answers 2

0

Try :

 $scope.clear = function() {
     $scope.model.value1 = '';
 };
Sign up to request clarification or add additional context in comments.

Comments

0

You can't clear because your pattern make angular validate it. If it does not match the pattern, your model wont be updated which is why clearing it do nothing since its already undefined.

With ng-pattern, $setPristine() will reset the value but will not trigger default browser validation (red border, error message around input) so you have to trigger it yourself.

2 Comments

Yes, I've already understood that, but i need way to clear view of the form on some "special" button click.
I just added a partial solution

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.