继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

AngularJS 自定义指令

LEATH
关注TA
已关注
手记 315
粉丝 93
获赞 466

AngularJS 指令除了基本了的 app、init、model、repeat我们还可以调用自己定义的指令,调用方式有四种:

E :元素名
A:属性
C:类名
M:注释

  1. 元素名

<!DOCTYPE html><html><head>
    <meta charset="utf-8">
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body ng-app="app"><test></test><script>
    var app = angular.module("app", []);
    app.directive("test", function () {        return {
            restrict: "E",   //可省略,默认是EA,即元素名和属性
            template: "这是自定义的通过元素名调用"
        }
    })</script></body></html>//这是自定义的通过元素名调用
  1. 属性

<body ng-app="app"><div test></div><script>
    var app = angular.module("app", []);
    app.directive("test", function () {        return {
            restrict: "A",   //可省略,默认是EA,即元素名和属性
            template: "这是自定义的通过属性调用"
        }
    })</script></body>//这是自定义的通过属性调用
  1. 类名

<body ng-app="app"><div class=" test"></div><script>
    var app = angular.module("app", []);
    app.directive("test", function () {        return {
            restrict: "C", 
            template: "这是自定义的通过类名调用"
        }
    })</script></body>//这是自定义的通过类名调用
  1. 注释

<body ng-app="app"> <!-- directive: test -->

 <script>
    var app = angular.module("app", []);
    app.directive("test", function () {        return {
            restrict: "M",
            replace : true,
            template: "<h1>这是自定义的通过注释调用</h1>"
        }
    })</script></body>//这是自定义的通过注释调用

PS:一定要在 test 左右留空格;



作者:H_婷
链接:https://www.jianshu.com/p/8e5109c67a2a


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP