Upgrade to angularjs 1.2.0 rc1
This commit is contained in:
parent
d223dfd662
commit
d6b021bfaf
674 changed files with 79667 additions and 62269 deletions
149
lib/angular/docs/partials/guide/forms.html
Normal file → Executable file
149
lib/angular/docs/partials/guide/forms.html
Normal file → Executable file
|
@ -1,25 +1,22 @@
|
|||
<h1><code ng:non-bindable=""></code>
|
||||
<span class="hint"></span>
|
||||
<a href="http://github.com/angular/angular.js/edit/master/docs/content/guide/forms.ngdoc" class="improve-docs btn btn-primary"><i class="icon-edit"> </i> Improve this doc</a><h1><code ng:non-bindable=""></code>
|
||||
<div><span class="hint"></span>
|
||||
</div>
|
||||
</h1>
|
||||
<div><a href="http://github.com/angular/angular.js/edit/master/docs/content/guide/forms.ngdoc" class="improve-docs btn btn-primary">Improve this doc</a><p>Controls (<code>input</code>, <code>select</code>, <code>textarea</code>) are a way for user to enter data.
|
||||
<div><div class="forms-page"><p>Controls (<code>input</code>, <code>select</code>, <code>textarea</code>) are a way for user to enter data.
|
||||
Form is a collection of controls for the purpose of grouping related controls together.</p>
|
||||
|
||||
<p>Form and controls provide validation services, so that the user can be notified of invalid input.
|
||||
This provides a better user experience, because the user gets instant feedback on how to correct the error.
|
||||
Keep in mind that while client-side validation plays an important role in providing good user experience, it can easily be circumvented and thus can not be trusted.
|
||||
Server-side validation is still necessary for a secure application.</p>
|
||||
|
||||
<h2>Simple form</h2>
|
||||
|
||||
<h2>Simple form</h1>
|
||||
<p>The key directive in understanding two-way data-binding is <a href="api/ng.directive:ngModel"><code>ngModel</code></a>.
|
||||
The <code>ngModel</code> directive provides the two-way data-binding by synchronizing the model to the view, as well as view to the model.
|
||||
In addition it provides an <a href="api/ng.directive:ngModel.NgModelController"><code>API</code></a> for other directives to augment its behavior.</p>
|
||||
|
||||
<h3>Source</h3>
|
||||
<div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-51" source-edit-css="" source-edit-js="script.js-50" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<h3>Source</h2>
|
||||
<div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-224" source-edit-css="" source-edit-js="script.js-223" source-edit-json="" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<div class="tabbable"><div class="tab-pane" title="index.html">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-51" ng-html-wrap=" angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-51">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-224" ng-html-wrap=" angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-224">
|
||||
<div ng-controller="Controller">
|
||||
<form novalidate class="simple-form">
|
||||
Name: <input type="text" ng-model="user.name" /><br />
|
||||
|
@ -37,8 +34,8 @@ In addition it provides an <a href="api/ng.directive:ngModel.NgModelController">
|
|||
</script>
|
||||
</div>
|
||||
<div class="tab-pane" title="script.js">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-50"></pre>
|
||||
<script type="text/ng-template" id="script.js-50">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-223"></pre>
|
||||
<script type="text/ng-template" id="script.js-223">
|
||||
function Controller($scope) {
|
||||
$scope.master= {};
|
||||
|
||||
|
@ -54,31 +51,25 @@ In addition it provides an <a href="api/ng.directive:ngModel.NgModelController">
|
|||
}
|
||||
</script>
|
||||
</div>
|
||||
</div><h3>Demo</h3>
|
||||
<div class="well doc-example-live animator-container" ng-embed-app="" ng-set-html="index.html-51" ng-eval-javascript="script.js-50"></div>
|
||||
|
||||
<p>Note that <code>novalidate</code> is used to disable browser's native form validation.</p>
|
||||
|
||||
<h2>Using CSS classes</h2>
|
||||
|
||||
</div><h2>Demo</h2>
|
||||
<div class="well doc-example-live animate-container" ng-embed-app="" ng-set-html="index.html-224" ng-eval-javascript="script.js-223"></div>
|
||||
<p>Note that <code>novalidate</code> is used to disable browser's native form validation.</p>
|
||||
<h1>Using CSS classes</h1>
|
||||
<p>To allow styling of form as well as controls, <code>ngModel</code> add these CSS classes:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>ng-valid</code></li>
|
||||
<li><code>ng-invalid</code></li>
|
||||
<li><code>ng-pristine</code></li>
|
||||
<li><code>ng-dirty</code></li>
|
||||
</ul>
|
||||
|
||||
<p>The following example uses the CSS to display validity of each form control.
|
||||
In the example both <code>user.name</code> and <code>user.email</code> are required, but are rendered with red background only when they are dirty.
|
||||
This ensures that the user is not distracted with an error until after interacting with the control, and failing to satisfy its validity.</p>
|
||||
|
||||
<h3>Source</h3>
|
||||
<div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-53" source-edit-css="" source-edit-js="script.js-52" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<h2>Source</h2>
|
||||
<div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-226" source-edit-css="" source-edit-js="script.js-225" source-edit-json="" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<div class="tabbable"><div class="tab-pane" title="index.html">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-53" ng-html-wrap=" angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-53">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-226" ng-html-wrap=" angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-226">
|
||||
<div ng-controller="Controller">
|
||||
<form novalidate class="css-form">
|
||||
Name:
|
||||
|
@ -105,13 +96,13 @@ This ensures that the user is not distracted with an error until after interacti
|
|||
</script>
|
||||
</div>
|
||||
<div class="tab-pane" title="script.js">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-52"></pre>
|
||||
<script type="text/ng-template" id="script.js-52">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-225"></pre>
|
||||
<script type="text/ng-template" id="script.js-225">
|
||||
function Controller($scope) {
|
||||
$scope.master= {};
|
||||
$scope.master = {};
|
||||
|
||||
$scope.update = function(user) {
|
||||
$scope.master= angular.copy(user);
|
||||
$scope.master = angular.copy(user);
|
||||
};
|
||||
|
||||
$scope.reset = function() {
|
||||
|
@ -122,30 +113,25 @@ This ensures that the user is not distracted with an error until after interacti
|
|||
}
|
||||
</script>
|
||||
</div>
|
||||
</div><h3>Demo</h3>
|
||||
<div class="well doc-example-live animator-container" ng-embed-app="" ng-set-html="index.html-53" ng-eval-javascript="script.js-52"></div>
|
||||
|
||||
<h2>Binding to form and control state</h2>
|
||||
|
||||
<p>A form is in instance of <a href="api/ng.directive:form.FormController"><code>FormController</code></a>.
|
||||
</div><h2>Demo</h2>
|
||||
<div class="well doc-example-live animate-container" ng-embed-app="" ng-set-html="index.html-226" ng-eval-javascript="script.js-225"></div>
|
||||
<h1>Binding to form and control state</h1>
|
||||
<p>A form is an instance of <a href="api/ng.directive:form.FormController"><code>FormController</code></a>.
|
||||
The form instance can optionally be published into the scope using the <code>name</code> attribute.
|
||||
Similarly control is an instance of <a href="api/ng.directive:ngModel.NgModelController"><code>NgModelController</code></a>.
|
||||
The control instance can similarly be published into the form instance using the <code>name</code> attribute.
|
||||
This implies that the internal state of both the form and the control is available for binding in the view using the standard binding primitives.</p>
|
||||
|
||||
<p>This allows us to extend the above example with these features:</p>
|
||||
|
||||
<ul>
|
||||
<li>RESET button is enabled only if form has some changes</li>
|
||||
<li>SAVE button is enabled only if form has some changes and is valid</li>
|
||||
<li>custom error messages for <code>user.email</code> and <code>user.agree</code></li>
|
||||
</ul>
|
||||
|
||||
<h3>Source</h3>
|
||||
<div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-55" source-edit-css="" source-edit-js="script.js-54" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<h2>Source</h2>
|
||||
<div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-228" source-edit-css="" source-edit-js="script.js-227" source-edit-json="" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<div class="tabbable"><div class="tab-pane" title="index.html">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-55" ng-html-wrap=" angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-55">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-228" ng-html-wrap=" angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-228">
|
||||
<div ng-controller="Controller">
|
||||
<form name="form" class="css-form" novalidate>
|
||||
Name:
|
||||
|
@ -175,8 +161,8 @@ This implies that the internal state of both the form and the control is availab
|
|||
</script>
|
||||
</div>
|
||||
<div class="tab-pane" title="script.js">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-54"></pre>
|
||||
<script type="text/ng-template" id="script.js-54">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-227"></pre>
|
||||
<script type="text/ng-template" id="script.js-227">
|
||||
function Controller($scope) {
|
||||
$scope.master= {};
|
||||
|
||||
|
@ -196,43 +182,40 @@ This implies that the internal state of both the form and the control is availab
|
|||
}
|
||||
</script>
|
||||
</div>
|
||||
</div><h3>Demo</h3>
|
||||
<div class="well doc-example-live animator-container" ng-embed-app="" ng-set-html="index.html-55" ng-eval-javascript="script.js-54"></div>
|
||||
|
||||
<h2>Custom Validation</h2>
|
||||
|
||||
</div><h2>Demo</h2>
|
||||
<div class="well doc-example-live animate-container" ng-embed-app="" ng-set-html="index.html-228" ng-eval-javascript="script.js-227"></div>
|
||||
<h1>Custom Validation</h1>
|
||||
<p>Angular provides basic implementation for most common html5 <a href="api/ng.directive:input"><code>input</code></a>
|
||||
types: (<a href="api/ng.directive:input.text"><code>text</code></a>, <a href="api/ng.directive:input.number"><code>number</code></a>, <a href="api/ng.directive:input.url"><code>url</code></a>, <a href="api/ng.directive:input.email"><code>email</code></a>, <a href="api/ng.directive:input.radio"><code>radio</code></a>, <a href="api/ng.directive:input.checkbox"><code>checkbox</code></a>), as well as some directives for validation (<code>required</code>, <code>pattern</code>, <code>minlength</code>, <code>maxlength</code>, <code>min</code>, <code>max</code>).</p>
|
||||
|
||||
<p>Defining your own validator can be done by defining your own directive which adds a custom validation function to the <code>ngModel</code> <a href="api/ng.directive:ngModel.NgModelController"><code>controller</code></a>.
|
||||
To get a hold of the controller the directive specifies a dependency as shown in the example below.
|
||||
The validation can occur in two places:</p>
|
||||
|
||||
<ul>
|
||||
<li><p><strong>Model to View update</strong> -
|
||||
Whenever the bound model changes, all functions in <a href="api/ng.directive:ngModel.NgModelController#$formatters"><code>NgModelController#$formatters</code></a> array are pipe-lined, so that each of these functions has an opportunity to format the value and change validity state of the form control through <a href="api/ng.directive:ngModel.NgModelController#$setValidity"><code>NgModelController#$setValidity</code></a>.</p></li>
|
||||
Whenever the bound model changes, all functions in <a href="api/ng.directive:ngModel.NgModelController#$formatters"><code>NgModelController#$formatters</code></a> array are pipe-lined, so that each of these functions has an opportunity to format the value and change validity state of the form control through <a href="api/ng.directive:ngModel.NgModelController#$setValidity"><code>NgModelController#$setValidity</code></a>.</p>
|
||||
</li>
|
||||
<li><p><strong>View to Model update</strong> -
|
||||
In a similar way, whenever a user interacts with a control it calls <a href="api/ng.directive:ngModel.NgModelController#$setViewValue"><code>NgModelController#$setViewValue</code></a>.
|
||||
This in turn pipelines all functions in the <a href="api/ng.directive:ngModel.NgModelController#$parsers"><code>NgModelController#$parsers</code></a> array, so that each of these functions has an opportunity to convert the value and change validity state of the form control through <a href="api/ng.directive:ngModel.NgModelController#$setValidity"><code>NgModelController#$setValidity</code></a>.</p></li>
|
||||
This in turn pipelines all functions in the <a href="api/ng.directive:ngModel.NgModelController#$parsers"><code>NgModelController#$parsers</code></a> array, so that each of these functions has an opportunity to convert the value and change validity state of the form control through <a href="api/ng.directive:ngModel.NgModelController#$setValidity"><code>NgModelController#$setValidity</code></a>.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>In the following example we create two directives.</p>
|
||||
|
||||
<ul>
|
||||
<li><p>The first one is <code>integer</code> and it validates whether the input is a valid integer.
|
||||
For example <code>1.23</code> is an invalid value, since it contains a fraction.
|
||||
Note that we unshift the array instead of pushing.
|
||||
This is because we want to be first parser and consume the control string value, as we need to execute the validation function before a conversion to number occurs.</p></li>
|
||||
This is because we want to be first parser and consume the control string value, as we need to execute the validation function before a conversion to number occurs.</p>
|
||||
</li>
|
||||
<li><p>The second directive is a <code>smart-float</code>.
|
||||
It parses both <code>1.2</code> and <code>1,2</code> into a valid float number <code>1.2</code>.
|
||||
Note that we can't use input type <code>number</code> here as HTML5 browsers would not allow the user to type what it would consider an invalid number such as <code>1,2</code>.</p></li>
|
||||
Note that we can't use input type <code>number</code> here as HTML5 browsers would not allow the user to type what it would consider an invalid number such as <code>1,2</code>.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h3>Source</h3>
|
||||
<div source-edit="form-example1" source-edit-deps="angular.js script.js" source-edit-html="index.html-57" source-edit-css="" source-edit-js="script.js-56" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<h2>Source</h2>
|
||||
<div source-edit="form-example1" source-edit-deps="angular.js script.js" source-edit-html="index.html-230" source-edit-css="" source-edit-js="script.js-229" source-edit-json="" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<div class="tabbable"><div class="tab-pane" title="index.html">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-57" ng-html-wrap="form-example1 angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-57">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-230" ng-html-wrap="form-example1 angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-230">
|
||||
<div ng-controller="Controller">
|
||||
<form name="form" class="css-form" novalidate>
|
||||
<div>
|
||||
|
@ -258,8 +241,8 @@ Note that we can't use input type <code>number</code> here as HTML5 browsers wou
|
|||
</script>
|
||||
</div>
|
||||
<div class="tab-pane" title="script.js">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-56"></pre>
|
||||
<script type="text/ng-template" id="script.js-56">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-229"></pre>
|
||||
<script type="text/ng-template" id="script.js-229">
|
||||
var app = angular.module('form-example1', []);
|
||||
|
||||
var INTEGER_REGEXP = /^\-?\d*$/;
|
||||
|
@ -301,30 +284,23 @@ Note that we can't use input type <code>number</code> here as HTML5 browsers wou
|
|||
});
|
||||
</script>
|
||||
</div>
|
||||
</div><h3>Demo</h3>
|
||||
<div class="well doc-example-live animator-container" ng-embed-app="form-example1" ng-set-html="index.html-57" ng-eval-javascript="script.js-56"></div>
|
||||
|
||||
<h2>Implementing custom form controls (using <code>ngModel</code>)</h2>
|
||||
|
||||
</div><h2>Demo</h2>
|
||||
<div class="well doc-example-live animate-container" ng-embed-app="form-example1" ng-set-html="index.html-230" ng-eval-javascript="script.js-229"></div>
|
||||
<h1>Implementing custom form controls (using <code>ngModel</code>)</h2>
|
||||
<p>Angular implements all of the basic HTML form controls (<a href="api/ng.directive:input"><code>input</code></a>, <a href="api/ng.directive:select"><code>select</code></a>, <a href="api/ng.directive:textarea"><code>textarea</code></a>), which should be sufficient for most cases.
|
||||
However, if you need more flexibility, you can write your own form control as a directive.</p>
|
||||
|
||||
<p>In order for custom control to work with <code>ngModel</code> and to achieve two-way data-binding it needs to:</p>
|
||||
|
||||
<ul>
|
||||
<li>implement <code>$render</code> method, which is responsible for rendering the data after it passed the <a href="api/ng.directive:ngModel.NgModelController#$formatters"><code>NgModelController#$formatters</code></a>,</li>
|
||||
<li>call <code>$setViewValue</code> method, whenever the user interacts with the control and model needs to be updated. This is usually done inside a DOM Event listener.</li>
|
||||
</ul>
|
||||
|
||||
<p>See <a href="guide/directive">$compileProvider.directive</a> for more info.</p>
|
||||
|
||||
<p>The following example shows how to add two-way data-binding to contentEditable elements.</p>
|
||||
|
||||
<h3>Source</h3>
|
||||
<div source-edit="form-example2" source-edit-deps="angular.js script.js" source-edit-html="index.html-59" source-edit-css="" source-edit-js="script.js-58" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<h2>Source</h2>
|
||||
<div source-edit="form-example2" source-edit-deps="angular.js script.js" source-edit-html="index.html-232" source-edit-css="" source-edit-js="script.js-231" source-edit-json="" source-edit-unit="" source-edit-scenario=""></div>
|
||||
<div class="tabbable"><div class="tab-pane" title="index.html">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-59" ng-html-wrap="form-example2 angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-59">
|
||||
<pre class="prettyprint linenums" ng-set-text="index.html-232" ng-html-wrap="form-example2 angular.js script.js"></pre>
|
||||
<script type="text/ng-template" id="index.html-232">
|
||||
|
||||
|
||||
<div contentEditable="true" ng-model="content" title="Click to edit">Some</div>
|
||||
|
@ -339,14 +315,14 @@ However, if you need more flexibility, you can write your own form control as a
|
|||
</script>
|
||||
</div>
|
||||
<div class="tab-pane" title="script.js">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-58"></pre>
|
||||
<script type="text/ng-template" id="script.js-58">
|
||||
<pre class="prettyprint linenums" ng-set-text="script.js-231"></pre>
|
||||
<script type="text/ng-template" id="script.js-231">
|
||||
angular.module('form-example2', []).directive('contenteditable', function() {
|
||||
return {
|
||||
require: 'ngModel',
|
||||
link: function(scope, elm, attrs, ctrl) {
|
||||
// view -> model
|
||||
elm.bind('blur', function() {
|
||||
elm.on('blur', function() {
|
||||
scope.$apply(function() {
|
||||
ctrl.$setViewValue(elm.html());
|
||||
});
|
||||
|
@ -364,5 +340,6 @@ However, if you need more flexibility, you can write your own form control as a
|
|||
});
|
||||
</script>
|
||||
</div>
|
||||
</div><h3>Demo</h3>
|
||||
<div class="well doc-example-live animator-container" ng-embed-app="form-example2" ng-set-html="index.html-59" ng-eval-javascript="script.js-58"></div></div>
|
||||
</div><h2>Demo</h3>
|
||||
<div class="well doc-example-live animate-container" ng-embed-app="form-example2" ng-set-html="index.html-232" ng-eval-javascript="script.js-231"></div>
|
||||
</div></div>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue