• angularJS 之Directives


    The restrict option is typically set to:

    • 'A' - only matches attribute name
    • 'E' - only matches element name
    • 'AE' - matches either attribute or element name

    Let's change our directive to use restrict: 'E':

    1.

     index2.html

    1
    <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 </head> 6 <body> 7 <div ng-app="superhero"> 8 <superman></superman> 9 </div> 10 <script src="../angular-1.0.1.min.js"></script> 11 <script src="main2.js"></script> 12 </body> 13 </html>
    main2.js

    1
    var app=angular.module('superhero',[]); 2 app.directive('superman',function(){ 3 return{ 4 restrict:"E", 5 template:"<div>Here I am to save the day</div>" 6 } 7 });

    2.

    Let's change our directive to use restrict: 'A':

     index2.html

    1
    <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 </head> 6 <body> 7 <div ng-app="superhero"> 8 <div superman></div> 9 </div> 10 <script src="../angular-1.0.1.min.js"></script> 11 <script src="main2.js"></script> 12 </body> 13 </html>
    main2.js

    1
    var app=angular.module('superhero',[]); 2 app.directive('superman',function(){ 3 return{ 4 restrict:"A", 5 link:function(){alert("I'm working")}6 } 7 });

    3.

    Let's change our directive to use restrict: 'C':

     index2.html

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 </head> 6 <body> 7 <div ng-app="superhero"> 8 <div class="superman"></div> 9 </div> 10 <script src="../angular-1.0.1.min.js"></script> 11 <script src="main2.js"></script> 12 </body> 13 </html>
    main2.js

    1 var app=angular.module('superhero',[]); 2 app.directive('superman',function(){ 3 return{ 4 restrict:"C", 5 link:function(){alert("I'm working")} 6 } 7 });

    4.

     index.html
    1
    <!doctype html> 2 <html ng-app="docsRestrictDirective"> 3 <head> 4 <script src="http://code.angularjs.org/1.2.7/angular.min.js"></script> 5 <script src="script.js"></script> 6 </head> 7 <body> 8 <div ng-controller="Ctrl"> 9 <my-customer></my-customer> 10 </div> 11 </body> 12 </html>
    customer.html
    Name: {{customer.name}} Address: {{customer.address}}
    
    
    script.js
     1 angular.module('docsRestrictDirective', [])
     2   .controller('Ctrl', function($scope) {
     3     $scope.customer = {
     4       name: 'Naomi',
     5       address: '1600 Amphitheatre'
     6     };
     7   })
     8   .directive('myCustomer', function() {
     9     return {
    10       restrict: 'E',
    11       templateUrl: 'customer.html'
    12     };
    13   });

    templateUrl:'my-customer.html'

    template:'Name: {{customer.name}} Address: {{customer.address}}'

    5.

     1 <!DOCTYPE html>
     2  <html>
     3 <head>
     4          <title></title>
     5      </head>
     6  <body>
     7  <div ng-app="behaviorApp">
     8   <div enter leave>aaa</div>
     9     </div>
    10  <script src="../angular-1.0.1.min.js"></script>
    11  <script src="main3.js"></script>
    12  </body>
    13  </html>
    main3.js

     1 /**
     2  * 
     3  */
     4 var app=angular.module('behaviorApp',[]);
     5  app.directive('enter',function(){
     6      return function(scope,element){
     7          element.bind("mouseenter",function(){
     8              console.info("I'm inside of you!");
     9          });
    10      }
    11  });
    12 
    13 app.directive('leave',function(){
    14     return function(scope,element){
    15         element.bind("mouseleave",function(){
    16             console.info("I'm leaving on a jet plane!");
    17         });
    18     }
    19 });

    6.

     1 <!DOCTYPE html>
     2  <html>
     3 <head>
     4          <title></title>
     5      </head>
     6  <body>
     7  <div ng-app="behaviorApp">
     8   <div enter="panel" leave>aaa</div>
     9     </div>
    10  <script src="../angular-1.0.1.min.js"></script>
    11  <script src="main3.js"></script>
    12  </body>
    13  </html>
     main3.js
    1
    /** 2 * 3 */ 4 var app=angular.module('behaviorApp',[]); 5 app.directive('enter',function(){ 6 return function(scope,element){ 7 element.bind("mouseenter",function(){ 8 element.addClass("panel");//添加 9 }); 10 } 11 }); 12 13 app.directive('leave',function(){ 14 return function(scope,element,attrs){ 15 element.bind("mouseleave",function(){ 16 element.removeClass(attrs.enter);//arrts.enter===panel 移除 17 }); 18 } 19 });

    function link(scope, element, attrs) { ... } where:

    • scope is an Angular scope object.
    • element is the jqLite-wrapped element that this directive matches.
    • attrs is an object with the normalized attribute names and their corresponding values.
  • 相关阅读:
    如何将baidu地图中的baidu logo 去掉
    漂亮的圆角文本框 CSS3实现
    jQuery数字加减插件
    腾迅股票数据接口 http/javascript
    JS复制内容到剪贴板(兼容FF/Chrome/Safari所有浏览器)
    关于编写性能高效的javascript事件的技术
    想做web开发 就学JavaScript
    git的简单理解及基础操作命令
    《CSS权威指南》基础复习+查漏补缺
    TypeScript Writing .d.ts files(编写声明文件)
  • 原文地址:https://www.cnblogs.com/yuluhuang/p/3504913.html
Copyright © 2020-2023  润新知