This article mainly introduces AngularJs Forms. Here we have compiled relevant information and simple sample codes. Friends in need can refer to it
Controls (input, select, textarea) are a way for users to input data. A Form is a collection of these controls designed to group related controls.
Forms and controls provide validation services, so users can receive prompts for invalid input. This provides a better user experience because users can get immediate feedback and know how to correct errors. Keep in mind that while client-side validation plays an important role in providing a good user experience, it can be easily bypassed and, therefore, cannot be trusted. Server-side authentication is still necessary for a secure application.
1. Simple form
The key directive to understand two-way data binding is ngModel. ngModel provides two-way data binding from model to view and view to model. Moreover, it also provides APIs to other directives to enhance their behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 |
|
2. Using CSS classes
In order to make the form, control, and ngModel rich Style, you can add the following class:
ng-valid
ng-invalid
ng -pristine (never entered)
ng-dirty (entered)
The following example uses CSS to display Validity of each form control. In the example, user.name and user.email are both required, but when they are modified (dirty), the background will appear red. This ensures that the user is not distracted by an error until after interacting with the form (after submission?) and discovering that its validity was not met.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
1 |
|
3. Binding to form and control state
In angular, the form is FormController An instance. The form instance can be exposed to the scope at will using the name attribute (I don't understand here, there is no property in the scope that is the same as the name attribute of the form, but because of the "document.form name" method, it can still be obtained ). Similarly, controls are instances of NgModelController. Control instances can similarly be exposed to forms using the name attribute. This shows that the internal properties of both the form and the control (control) are feasible for binding in the view using standard binding primitives.
This allows us to extend the above example with the following feature:
The RESET button is only available after the form has changed.
The SAVE button is only available when the form changes and the input is valid.
Customize error messages for user.email and user.agree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
1 |
|
4. Custom Validation
Angular is most common HTML form field (input, text, number, url, email, radio, checkbox) types provide implementations, and there are also some directives (required, pattern,, inlength, maxlength, min, max) for form validation.
We can define our own validation plug-in by defining a directive that adds a custom validation function to the ngModel controller (is this a connected ngModelController?). To get a controller, the directive specifies dependencies as shown in the following example (require attribute in the directive definition object).
Model to View update - Whenever the Model changes, all functions in the ngModelController.$formatters (data validation and format conversion are triggered when the model changes) array will be queued for execution, so in Each function here has the opportunity to format the value of the model and pass NgModelController.$setValidity (http://code.angularjs.org/1.0.2/docs/api/ng.directive:ngModel.NgModelController#$setValidity ) to modify the validation status of the control.
View to Model update - A similar approach, whenever the user interacts with a control, NgModelController.$setViewValue will be triggered. At this time, it is the turn to execute NgModelController$parsers (after the control obtains the value from the DOM, it will execute all methods in this array, review, filter or convert the value, and also verify) all methods in the array.
In the following example we will create two directives.
The first one is integer, which is responsible for verifying whether the input is a valid integer. For example, 1.23 is an illegal value because it contains a decimal part. Note that we insert (unshift) at the head of the array instead of inserting (push) at the tail. This is because we want it to execute first and use the value of this control (it is estimated that this Array is used as a queue). We need to The validation function is executed before the conversion occurs.
第二个directive是smart-float。他将”1.2”和”1,2”转换为一个合法的浮点数”1.2”。注意,我们在这不可以使用HTML5的input类型”number”,因为浏览器不允许用户输入我们预期的非法字符,如”1,2”(它只认识”1.2”)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
五、Implementing custom form control (using ngModel)
angular实现了所有HTML的基础控件(input,select,textarea),能胜任大多数场景。然而,如果我们需要更加灵活,我们可以通过编写一个directive来实现自定义表单控件的目的。
为了制定控件和ngModel一起工作,并且实现双向数据绑定,它需要:
实现render方法,是负责在执行完并通过所有NgModelController.$formatters方法后,呈现数据的方法。
调用$setViewValue方法,无论任何时候用户与控件发生交互,model需要进行响应的更新。这通常在DOM事件监听器里实现。
可以查看$compileProvider.directive获取更多信息。
下面的例子展示如何添加双向数据绑定特性到可以编辑的元素中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of About the analysis of AngularJs Forms. For more information, please follow other related articles on the PHP Chinese website!