Require of AngularJs Directive directive

Source: Internet
Author: User
Tags float number

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
  1. var app = Angular.modeule (' MyApp ', []);
  2. App.directive (' common ', function () {
  3. return {
  4. ...
  5. Controller:function ($scope) {
  6. this.method1 = function () {
  7. };
  8. this.method2 = function () {
  9. };
  10. },
  11. ...
  12. }
  13. });
  14. App.directive (' D1 ', function () {
  15. return {
  16. ...
  17. Require: '? ^common ',
  18. Link:function (Scope,elem,attrs,common) {
  19. Scope.method1 = common.method1;
  20. ..
  21. },
  22. ...
  23. }
  24. });

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
  1. Angular.module (' myApp ')
  2. . directive (' Spoint ', function () {
  3. return {
  4. Require: ' Ngmodel ',
  5. Link:function (Scope, Elm, Attrs, ctrl) {
  6. var Fibonacci = [1, 2, 3, 5, 8,, +, 80];
  7. Ctrl. $parsers. Unshift (function (viewvalue) {
  8. if (Fibonacci.indexof (parseint (viewvalue)) >= 0) {
  9. Ctrl. $setValidity (' Fibonacci ', true);
  10. return viewvalue;
  11. } Else {
  12. Ctrl. $setValidity (' Fibonacci ', false);
  13. return undefined;
  14. }
  15. });
  16. }
  17. };
  18. });

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
    1. <input type="text" ng-model="Length" name="Length" smart-float/>
    2. {{length}}<br/>
    3. <span ng-show="form.length. $error. Float" >this is not a valid float number!</span>

Js

Java code
  1. var float_regexp =/^\-?\d+ ((\.| \,) \d+)? $/;
  2. App.directive (' smartfloat ', function () {
  3. return {
  4. Require: ' Ngmodel ',
  5. Link:function (Scope, Elm, Attrs, ctrl) {
  6. Ctrl. $parsers. Unshift (function (viewvalue) {
  7. if (float_regexp.test (Viewvalue)) {
  8. Ctrl. $setValidity (' float ', true);
  9. return parsefloat (Viewvalue.replace (', ', ' . '));
  10. } Else {
  11. Ctrl. $setValidity (' float ', false);
  12. return undefined;
  13. }
  14. });
  15. }
  16. };
  17. });

Require of AngularJs Directive directive

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.