The use of controller is divided into two situations, one is require custom controller, because the property method in custom controller is written by oneself, it is simpler to be used, and the other method is require ANGULARJS built-in instruction , most of the time it takes require to ngmodel this instruction.
When customizing the angular directive, there is a field called require, which is used to communicate with each other between instructions. As a simple example, if we need to write two instructions now, there are many coincident methods in the linking function, in order to avoid repeating ourselves (the famous dry principle), we can write this repeating method in the controller of the third instruction, It then require the command with the Controller field in the other two required instructions, and the coincident method can be referenced by the fourth parameter of the linking function. The structure of the code is roughly as follows:
Java code
- var app = Angular.modeule (' MyApp ', []);
- App.directive (' common ', function () {
- return {
- ...
- Controller:function ($scope) {
- this.method1 = function () {
- };
- this.method2 = function () {
- };
- },
- ...
- }
- });
- App.directive (' D1 ', function () {
- return {
- ...
- Require: '? ^common ',
- Link:function (Scope,elem,attrs,common) {
- Scope.method1 = common.method1;
- ..
- },
- ...
- }
- });
Of course, the above example is only one of the controller usages in the instruction. Although generally speaking, the opportunity to use the Controller field is not many, but to write good angularjs instructions, this is a point that must be mastered.
Referencing built-in directives
Java code
- Angular.module (' myApp ')
- . directive (' Spoint ', function () {
- return {
- Require: ' Ngmodel ',
- Link:function (Scope, Elm, Attrs, ctrl) {
- var Fibonacci = [1, 2, 3, 5, 8,, +, 80];
- Ctrl. $parsers. Unshift (function (viewvalue) {
- if (Fibonacci.indexof (parseint (viewvalue)) >= 0) {
- Ctrl. $setValidity (' Fibonacci ', true);
- return viewvalue;
- } Else {
- Ctrl. $setValidity (' Fibonacci ', false);
- return undefined;
- }
- });
- }
- };
- });
Note Here is the fourth parameter of the directive link method, we define the Ngmodel in require so here it is a Ngmodelcontroller
Ngmodelcontroller is used to provide a set of APIs for Ng-model. Through him we can him to determine whether the value of the Ngmodel is legal. We'll cover only a few of the methods and properties that are related to form validation.
In the above example, we used the $parsers attribute and the $setvalidity () method. A set of function is saved in the $parsers, which is called once when the data in the DOM changes. This gives us the opportunity to check the value of the new input when the user modifies the value in the DOM.
"Smart floating point (smart-float)" directive. It can convert "1.2" or "all" to a valid floating-point number "1.2". Note that we cannot use the "digital input type" here, because HTML5 's browser does not allow users to enter illegal values such as "."
Html
Java code
- <input type="text" ng-model="Length" name="Length" smart-float/>
- {{length}}<br/>
- <span ng-show="form.length. $error. Float" >this is not a valid float number!</span>
Js
Java code
- 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;
- }
- });
- }
- };
- });
Require of AngularJs Directive directive