zoukankan      html  css  js  c++  java
  • angularjs 创建自定义的指令

    创建自定义的指令

    除了 AngularJS 内置的指令外,我们还可以创建自定义指令。

    你可以使用 .directive 函数来添加自定义的指令。

    要调用自定义指令,HTMl 元素上需要添加自定义指令名。

    使用驼峰法来命名一个指令, runoobDirective, 但在使用它时需要以 - 分割, runoob-directive:

    你可以通过以下方式来调用指令:

    • 元素名
    • 属性
    • 类名
    • 注释

    以下实例方式也能输出同样结果:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>

    <body ng-app="myApp">

    <p>元素名</p>
    <runoob-directive></runoob-directive>

    <p>类名</p>
    <div class="class-directive"></div>

    <p>属性</p>
    <div attribute-directive></div>

    <p>注释</p>
    <!-- directive: commance-directive -->
    <script>
    var app = angular.module("myApp", []);
    app.directive("runoobDirective", function() {
    return {
    template : "<div><ul><li>1</li><li>1</li><li>1</li><li>1</li></ul></div>"
    };
    });

    app.directive("classDirective", function() {
    return {
    restrict: "C",
    template : "<div><ul><li>must set the restrict : "C"</li><li>class directive</li><li>class directive</li><li>class directive</li></ul></div>"
    };
    });

    app.directive("attributeDirective", function() {
    return {
    template : "<div><ul><li>Attribute directive</li><li>Attribute directive</li></ul></div>"
    };
    });

    app.directive("commanceDirective", function() {
    return {
    restrict : "M",
    replace : true,
    template : "<div><ul><li>commance directive</li><li>commance directive</li></ul></div>"
    };
    });

    </script>

    </body>

    </body>
    </html>

    限制使用

    你可以限制你的指令只能通过特定的方式来调用。

    • E 只限元素名使用
    • A 只限属性使用
    • C 只限类名使用
    • M 只限注释使用

    restrict 默认值为 EA, 即可以通过元素名和属性名来调用指令。

  • 相关阅读:
    面试题19:包含min函数的栈
    编程之美 计算字符串的相似度
    android 数据持久化——I/O操作
    SSD磁盘,CPU居高不下,高并发的情况下,是不是mysql解析器耗费的cpu资源高?
    Eclipse、MyEclipse优化,提高运行速度
    Sonar入门学习
    Oracle 生成指定范围内随机日期
    ios中的GCD
    如何使用Win8系统自带杀毒软件
    安装Ubuntu版本linux过程中没有提示设置root用户密码问题的解决办法
  • 原文地址:https://www.cnblogs.com/facial/p/5465895.html
Copyright © 2011-2022 走看看