Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ng-model needed when using ng-disabled and $invalid on a form?

Tags:

angularjs

I'm using AngularJS and have a form where I want the Submit button to be disabled if some fields are not filled in.

The standard way seems to be the following:

<form ng-app name="myForm">
    <label>Name</label>
    <input type="text" name="name" ng-model="form.name" required>
    <input type="submit" ng-disabled="myForm.name.$invalid">
</form>

http://jsfiddle.net/YMSRU/

However, if I omit the model from the input field the validation doesn't work and I don't need any models on my input fields (I submit my form using the ngUpload directive so it's actually sent to the form action in an iframe).

Is there any solution or should I add random models just to make the validation work? It seems like a bad work-around.

like image 642
user1767586 Avatar asked Oct 21 '22 09:10

user1767586


1 Answers

You could simply do the invalid check at the form level, then no need to define a model for each input:

<form ng-app name="myForm">
    <label>Name</label>
    <input type="text" name="name" required>
    <input type="submit" ng-disabled="myForm.$invalid">
</form>
like image 188
laurent Avatar answered Oct 27 '22 09:10

laurent