Example of event binding and instruction interaction usage for directive directives in Angularjs _angularjs

Source: Internet
Author: User


This article describes the event bindings and instruction interaction usages used by directive directives in Angularjs. Share to everyone for your reference, specific as follows:



Use of templates in Angularjs, event bindings, and interaction between directives and directives


<! doctype html>
<html ng-app = "myapp">
  <head>
    <meta charset = "utf-8" />
  </ head>
  <body ng-controller = "ShieldController">
    <div>
      <who> </ who>
    </ div>
    <div>
      <button you-btn> </ button>
    </ div>
    <theshield reigns> 343 </ theshield>
    <theshield reigns> fdhg </ theshield>
    <theshield rollins> hhh </ theshield>
    <theshield ambros> kkk </ theshield>
  </ body>
  <script src = "./ js / angular.min.js"> </ script>
  <script>
    var app = angular.module ('myapp', []);
    / * ======================= 1. Use of the template ==================== ==== * /
    app.directive ('who', function () {
      return {
        restrict: "E", // The meaning of element
        link: function (scope, element, attrs) {
          console.log (element);
          element [0] .innerHTML = 'sdfhkj'; // This has the highest priority
        },
        //templateUrl:"param.html ", // this is not displayed, the lowest priority
        template: "<h1> jkdhf </ h1>" // The priority of this display is next
      };
    });
    / * ======================= 2. Event binding =================== ===== * /
    app.directive ('youBtn', function () {
      return {
        restrict: "A", // attribute attribute meaning
        link: function (scope, element, attrs) {
          console.log (element);
          element [0] .innerHTML = 'my btn';
          // Event binding
          element.bind ('mouseenter', function () {
            element [0] .innerHTML = 'your btn';
          });
          element.bind ('mouseleave', function () {
            element [0] .innerHTML = 'her btn';
          });
        }
      };
    });
    / * ======================= 3. Interaction between element attribute controllers =============== ========= * /
    app.controller ('ShieldController', function ($ scope) {
      $ scope.shieldNames = [];
      this.addReigns = function () {
        $ scope.shieldNames.push ("reigns: jjj");
      }
      this.addRollins = function () {
        $ scope.shieldNames.push ("Rollins: hhh");
      }
      this.addAmbros = function () {
        $ scope.shieldNames.push ("Ambros: ggg");
      }
    })
    .directive ('reigns', function () {
     return {
       require: "theshield",
       link: function (scope, element, attrs, ShieldController) {
         ShieldController.addReigns ();
       }
     };
    })
    .directive ('rollins', function () {
     return {
       require: "theshield",
       link: function (scope, element, attrs, ShieldController) {
         ShieldController.addRollins ();
       }
     };
    })
    .directive ('ambros', function () {
     return {
       require: "theshield",
       link: function (scope, element, attrs, ShieldController) {
         ShieldController.addAmbros ();
       }
     };
    })
    .directive ('theshield', function () {
      return {
        restrict: "E",
        controller: "ShieldController", // Specify the controller
        scope: {}, // Empty the $ scope value at this instruction
        link: function (scope, element, attrs) {
          element.bind ('mouseenter', function () {// For the element corresponding to the instruction, bind the corresponding event
            console.log (scope.shieldNames);
          });
        }
      };
    });
  </ script>
</ html> 


I hope this article will help you with ANGULARJS programming.


Related Article

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.