Validator Instruction Implementation

Source: Internet
Author: User

Recently read the project code to learn how predecessors write angular instructions. No key business code is involved, so there should be no confidentiality issues. I hope to write this blog to understand how the validator instructions are implemented.

Use it like this:

The implementation of the checksum function relies on provider validator and several directive. The structure of the provider is this:

. Provider (' validator ', [function ()} {

var validatorfn = function () {//...};

Validatorfn.prototype = {//...};

var validator = new Validatorfn ();

this. $get = function () {return validator;}

}]);

1. form-validator directive

The main function of this directive: ① binding Defocus Check, execute dovalidate; ② Remove Error style ③ define Dovalidate function when Viewvalue changes

Scope. $apply (Scope[formname]. $errors);//What are you doing here?

1) Each directive instance is executed one link at a time.  

2) If the instruction does not have require CTRL and you define CTRL itself, the link is passed in its own defined CTRL.

3) The form has a length, which is the number of form elements.

4) Watch array changes (referenced from: http://anjianshi.net/post/yan-jiu-bi-ji/ng-watch):

5) The This in link points to the element itself

6) Scope[formname]: value is Formcontroller. Scope[formname]. $setPristine ()--Reset form

7) formname.elementname. $viewValue, Formname.elementname. $valid

2. Form-submit command: Execute dovalidate at time of submission

3.datetimecheck:

Read element value: Attr.ngmodel

Check time interval: if (time error) ctrl. $setValidity (Validationname, false); At this point, ctrl. {Validationname:true} will be present in the $error

4. Form-element

The form elements in the ng-repeat must be bound to form-element for the checksum to take effect because ng-repeat generates a new scope, and the Defocus check that is bound in the Form-validator scope does not apply.

The scope in Form-validator is the parent scope, and Ng-repeat generates a new isolation scope.

In summary, Datetimechecker, required, and other instructions custom methods execute $setvalidity (), Form-submit, and when the element is out of focus (Form-validator settings) executes dovalidate to read Ctrl . $error to modify the style.

Validator Provider Set the default rules, open SetRules interfaces, and Removeerror, ShowError, and other basic methods.

Validator Instruction Implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.