EVOLUTION-MANAGER
Edit File: form.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/src/ng/directive/form.js?message=docs(form)%3A%20describe%20your%20change...#L302' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <a href='https://github.com/angular/angular.js/tree/v1.3.9/src/ng/directive/form.js#L302' class='view-source pull-right btn btn-primary'> <i class="glyphicon glyphicon-zoom-in"> </i>View Source </a> <header class="api-profile-header"> <h1 class="api-profile-header-heading">form</h1> <ol class="api-profile-header-structure naked-list step-list"> <li> - directive in module <a href="api/ng">ng</a> </li> </ol> </header> <div class="api-profile-description"> <p>Directive that instantiates <a href="api/ng/type/form.FormController">FormController</a>.</p> <p>If the <code>name</code> attribute is specified, the form controller is published onto the current scope under this name.</p> <h1 id="alias-link-ng-directive-ngform-ngform-">Alias: <a href="api/ng/directive/ngForm"><code>ngForm</code></a></h1> <p>In Angular forms can be nested. This means that the outer form is valid when all of the child forms are valid as well. However, browsers do not allow nesting of <code><form></code> elements, so Angular provides the <a href="api/ng/directive/ngForm"><code>ngForm</code></a> directive which behaves identically to <code><form></code> but can be nested. This allows you to have nested forms, which is very useful when using Angular validation directives in forms that are dynamically generated using the <a href="api/ng/directive/ngRepeat"><code>ngRepeat</code></a> directive. Since you cannot dynamically generate the <code>name</code> attribute of input elements using interpolation, you have to wrap each set of repeated inputs in an <code>ngForm</code> directive and nest these in an outer <code>form</code> element.</p> <h1 id="css-classes">CSS classes</h1> <ul> <li><code>ng-valid</code> is set if the form is valid.</li> <li><code>ng-invalid</code> is set if the form is invalid.</li> <li><code>ng-pristine</code> is set if the form is pristine.</li> <li><code>ng-dirty</code> is set if the form is dirty.</li> <li><code>ng-submitted</code> is set if the form was submitted.</li> </ul> <p>Keep in mind that ngAnimate can detect each of these classes when added and removed.</p> <h1 id="submitting-a-form-and-preventing-the-default-action">Submitting a form and preventing the default action</h1> <p>Since the role of forms in client-side Angular applications is different than in classical roundtrip apps, it is desirable for the browser not to translate the form submission into a full page reload that sends the data to the server. Instead some javascript logic should be triggered to handle the form submission in an application-specific way.</p> <p>For this reason, Angular prevents the default action (form submission to the server) unless the <code><form></code> element has an <code>action</code> attribute specified.</p> <p>You can use one of the following two ways to specify what javascript method should be called when a form is submitted:</p> <ul> <li><a href="api/ng/directive/ngSubmit">ngSubmit</a> directive on the form element</li> <li><a href="api/ng/directive/ngClick">ngClick</a> directive on the first button or input field of type submit (input[type=submit])</li> </ul> <p>To prevent double execution of the handler, use only one of the <a href="api/ng/directive/ngSubmit">ngSubmit</a> or <a href="api/ng/directive/ngClick">ngClick</a> directives. This is because of the following form submission rules in the HTML specification:</p> <ul> <li>If a form has only one input field then hitting enter in this field triggers form submit (<code>ngSubmit</code>)</li> <li>if a form has 2+ input fields and no buttons or input[type=submit] then hitting enter doesn't trigger submit</li> <li>if a form has one or more input fields and one or more buttons or input[type=submit] then hitting enter in any of the input fields will trigger the click handler on the <em>first</em> button or input[type=submit] (<code>ngClick</code>) <em>and</em> a submit handler on the enclosing form (<code>ngSubmit</code>)</li> </ul> <p>Any pending <code>ngModelOptions</code> changes will take place immediately when an enclosing form is submitted. Note that <code>ngClick</code> events will occur before the model is updated. Use <code>ngSubmit</code> to have access to the updated model.</p> <h2 id="animation-hooks">Animation Hooks</h2> <p>Animations in ngForm are triggered when any of the associated CSS classes are added and removed. These classes are: <code>.ng-pristine</code>, <code>.ng-dirty</code>, <code>.ng-invalid</code> and <code>.ng-valid</code> as well as any other validations that are performed within the form. Animations in ngForm are similar to how they work in ngClass and animations can be hooked into using CSS transitions, keyframes as well as JS animations.</p> <p>The following example shows a simple way to utilize CSS transitions to style a form element that has been rendered as invalid after it has been validated:</p> <pre> //be sure to include ngAnimate as a module to hook into more //advanced animations .my-form { transition:0.5s linear all; background: white; } .my-form.ng-invalid { background: red; color:white; } </pre> </div> <div> <h2>Directive Info</h2> <ul> <li>This directive executes at priority level 0.</li> </ul> <h2 id="usage">Usage</h2> <div class="usage"> <ul> <li>as element: <pre><code><form [name=""]> ... </form></code></pre> </li> </div> <section class="api-section"> <h3>Arguments</h3> <table class="variables-matrix input-arguments"> <thead> <tr> <th>Param</th> <th>Type</th> <th>Details</th> </tr> </thead> <tbody> <tr> <td> name <div><em>(optional)</em></div> </td> <td> <a href="" class="label type-hint type-hint-string">string</a> </td> <td> <p>Name of the form. If specified, the form controller will be published into related scope, under this name.</p> </td> </tr> </tbody> </table> </section> <h2 id="example">Example</h2><p> <div> <a ng-click="openPlunkr('examples/example-example58')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-example58" deps="angular-animate.js" animations="true" fixBase="true" module="formExample"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><script> angular.module('formExample', []) .controller('FormController', ['$scope', function($scope) { $scope.userType = 'guest'; }]); </script> <style> .my-form { -webkit-transition:all linear 0.5s; transition:all linear 0.5s; background: transparent; } .my-form.ng-invalid { background: red; } </style> <form name="myForm" ng-controller="FormController" class="my-form"> userType: <input name="input" ng-model="userType" required> <span class="error" ng-show="myForm.input.$error.required">Required!</span><br> <tt>userType = {{userType}}</tt><br> <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br> <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br> <tt>myForm.$valid = {{myForm.$valid}}</tt><br> <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br> </form></code></pre> </div> <div class="runnable-example-file" name="protractor.js" type="protractor" language="js"> <pre><code>it('should initialize to model', function() { var userType = element(by.binding('userType')); var valid = element(by.binding('myForm.input.$valid')); expect(userType.getText()).toContain('guest'); expect(valid.getText()).toContain('true'); }); it('should be invalid if empty', function() { var userType = element(by.binding('userType')); var valid = element(by.binding('myForm.input.$valid')); var userInput = element(by.model('userType')); userInput.clear(); userInput.sendKeys(''); expect(userType.getText()).toEqual('userType ='); expect(valid.getText()).toContain('false'); });</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-example58/index.html" name="example-example58"></iframe> </div> </div> </p> </div>