From f7fad29fd99cabdbb17fa02dc214de2354f93cdf Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Sat, 15 Feb 2014 20:19:10 -0500 Subject: docs(bike-shed-migration): convert guide examples to ... This CL also contains style fixes as the converted scripts caused jshint to complain. --- docs/content/guide/forms.ngdoc | 430 ++++++++++++++++++++--------------------- 1 file changed, 215 insertions(+), 215 deletions(-) (limited to 'docs/content/guide/forms.ngdoc') diff --git a/docs/content/guide/forms.ngdoc b/docs/content/guide/forms.ngdoc index 2ef5e218..8a23c8c8 100644 --- a/docs/content/guide/forms.ngdoc +++ b/docs/content/guide/forms.ngdoc @@ -16,38 +16,38 @@ The key directive in understanding two-way data-binding is {@link ng.directive:n The `ngModel` 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 {@link ngModel.NgModelController API} for other directives to augment its behavior. - - -
-
- Name:
- E-mail:
- Gender: male - female
- - -
-
form = {{user | json}}
-
master = {{master | json}}
-
- - -
-
+ + +
+
+ Name:
+ E-mail:
+ Gender: male + female
+ + +
+
form = {{user | json}}
+
master = {{master | json}}
+
+ + +
+
Note that `novalidate` is used to disable browser's native form validation. @@ -67,47 +67,47 @@ The following example uses the CSS to display validity of each form control. In the example both `user.name` and `user.email` 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. - - -
-
- Name: -
- E-mail:
- Gender: male - female
- - -
-
- - - - -
-
+ + +
+
+ Name: +
+ E-mail:
+ Gender: male + female
+ + +
+
+ + + + +
+
@@ -130,54 +130,54 @@ This allows us to extend the above example with these features: - SAVE button is enabled only if form has some changes and is valid - custom error messages for `user.email` and `user.agree` - - -
-
- Name: -
- E-mail: -
-
Invalid: - Tell us your email. - This is not a valid email. + + +
+ + Name: +
+ E-mail: +
+
Invalid: + Tell us your email. + This is not a valid email. +
+ + Gender: male + female
+ + + I agree:
+
Please agree and sign.
+ + + +
+
- Gender: male - female
- - - I agree:
-
Please agree and sign.
- - - - -
+ + function Controller($scope) { + $scope.master = {}; - - - + $scope.reset(); + } + + @@ -209,72 +209,72 @@ In the following example we create two directives. Note that we can't use input type `number` here as HTML5 browsers would not allow the user to type what it would consider an invalid number such as `1,2`. - - -
-
-
- Size (integer 0 - 10): - {{size}}
- This is not valid integer! - - The value must be in range 0 to 10! -
- -
- Length (float): - - {{length}}
- - This is not a valid float number! + + +
+ +
+ Size (integer 0 - 10): + {{size}}
+ This is not valid integer! + + The value must be in range 0 to 10! +
+ +
+ Length (float): + + {{length}}
+ + This is not a valid float number! +
+
- -
- - - - + + + + var app = angular.module('form-example1', []); + + var INTEGER_REGEXP = /^\-?\d+$/; + app.directive('integer', function() { + return { + require: 'ngModel', + link: function(scope, elm, attrs, ctrl) { + ctrl.$parsers.unshift(function(viewValue) { + if (INTEGER_REGEXP.test(viewValue)) { + // it is valid + ctrl.$setValidity('integer', true); + return viewValue; + } else { + // it is invalid, return undefined (no model update) + ctrl.$setValidity('integer', false); + return undefined; + } + }); + } + }; + }); + + var FLOAT_REGEXP = /^\-?\d+((\.|\,)\d+)?$/; + app.directive('smartFloat', function() { + return { + require: 'ngModel', + link: function(scope, elm, attrs, ctrl) { + ctrl.$parsers.unshift(function(viewValue) { + if (FLOAT_REGEXP.test(viewValue)) { + ctrl.$setValidity('float', true); + return parseFloat(viewValue.replace(',', '.')); + } else { + ctrl.$setValidity('float', false); + return undefined; + } + }); + } + }; + }); + + # Implementing custom form controls (using `ngModel`) @@ -290,40 +290,40 @@ See {@link guide/directive $compileProvider.directive} for more info. The following example shows how to add two-way data-binding to contentEditable elements. - - - - -
Some
-
model = {{content}}
- - -
-
+ + + + + angular.module('form-example2', []).directive('contenteditable', function() { + return { + require: 'ngModel', + link: function(scope, elm, attrs, ctrl) { + // view -> model + elm.on('blur', function() { + scope.$apply(function() { + ctrl.$setViewValue(elm.html()); + }); + }); + + // model -> view + ctrl.$render = function() { + elm.html(ctrl.$viewValue); + }; + + // load init value from DOM + ctrl.$setViewValue(elm.html()); + } + }; + }); + + -- cgit v1.2.3