3

I am having problem with angular validation.
this one does'nt work...

<input type="text" ng-model="text1" name="text1" required>
<span ng-show="text1.$error.required">Please enter something!</span>

but this works:

<form name="myform">
<input type="text" ng-model="text1" name="text1" required>
<span ng-show="myform.text1.$error.required">Please enter something!</span>
</form>

is it possible to somehow correct the first one without placing it inside a form?

thanks

1
  • "is it possible to somehow correct the first one without placing it inside a form?" - No. Well.. It is actually possible, but not worth it, believe me, much simpler to do have form. Commented Oct 3, 2015 at 16:24

2 Answers 2

3

You can use the "ng-form" directive if you really dont want to add a form tag.

<body ng-controller="MainCtrl">
  <div ng-form="myForm">
    <input type="text" required ng-model="user.name" placeholder="Username">
    <button ng-click="doSomething()" ng-disabled="myForm.$invalid">DO</button>
  </div>
</body>

example

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

Comments

1

As far as I know, no, it is not possible. The FormController is what handles the states of each form element, so you need a reference to it in order to check the validation state.

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.