How can I conditionally require form inputs with AngularJS? How can I conditionally require form inputs with AngularJS? angularjs angularjs

How can I conditionally require form inputs with AngularJS?


There's no need to write a custom directive. Angular's documentation is good but not complete. In fact, there is a directive called ngRequired, that takes an Angular expression.

<input type='email'       name='email'       ng-model='contact.email'        placeholder='your@email.com'       ng-required='!contact.phone' /><input type='text'       ng-model='contact.phone'                    placeholder='(xxx) xxx-xxxx'       ng-required='!contact.email' />  

Here's a more complete example: http://jsfiddle.net/uptnx/1/


if you want put a input required if other is written:

   <input type='text'   name='name'   ng-model='person.name'/>   <input type='text'   ng-model='person.lastname'                ng-required='person.name' />  

Regards.


For Angular2

<input type='email'     [(ngModel)]='contact.email'    [required]='!contact.phone' >