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 Answers2

0

Try :

 $scope.clear = function() {
     $scope.model.value1 = '';
 };
Fr4ncx
  • 356
  • 6
  • 22
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.

ukn
  • 1,723
  • 1
  • 14
  • 24