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