0

My goal is to get rid of the error messages with a button click. So I thought I'd just clear the field on button click and hence, get ride of the error. It worked in my very first test.

<div class="errorPopup" 
data-ng-click="runner.Name = null" 
data-ng-show="mainForm.name.$error.pattern">
      <div class="triangle-down pull-left"></div>
      <span>Only letters allowed.</span>
</div>
<input ng-model="runner.Name" data-ng-patter="/(a-z)\" required />

But now I have to click once.. clearing the field. then click twice before the error div is deleted.

I'd like to get rid of the field and the div in one click

2
  • I don't think you've given us enough information to help you. It is not obvious to me what mainForm.name is; nor when it would change. Can you put together a plunker to show the problem? If I had to guess; you need to force a $digest.$apply() on the first click. Commented Aug 4, 2014 at 11:37
  • Don't try your code sample, but wouldn't it be easier using ng-pattern and not ng-patter? Commented Aug 4, 2014 at 11:45

1 Answer 1

1

There are a few issues with your code (assumign that there is a wrapping form with the name mainForm):

  • data-ng-patter should be data-ng-pattern

  • The regular expressions is not correct. If you want to only allow lowercase latin letters:
    /^[a-z]*$/

  • In order to check for an error in the input field (and reference it via mainForm.name.$error) the field must have a name. Since you are referring to it as mainForm.name it needs a name of name (which is not intuitive). Choose something representative (e.g. runnerName) and use it like this:

<input type="text" name="runnerName" ... />

<div ... ng-show="mainForm.runnerName.$error"> 

If you fix those issues, every seems to work fine !


See, also, this short demo.

Sign up to request clarification or add additional context in comments.

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.