自定义指令

来源:互联网 发布:知乎 swatch秒针掉了 编辑:程序博客网 时间:2024/06/06 13:17

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

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body ng-app="myApp"><runoob-directive></runoob-directive><script>    var app = angular.module("myApp", []);    app.directive("runoobDirective", function() {        return {            restrict : "E",            template : "<h1>自定义指令!</h1>"        };    });</script></body></html>

restrict 值可以是以下几种:

  • E 作为元素名使用
  • A 作为属性使用
  • C 作为类名使用
  • M 作为注释使用

restrict 默认值为 EA, 即可以通过元素名和属性名来调用指令。通过添加 restrict 属性,并设置只值为 “E”, 来设置指令只能通过元素名的方式来调用

0 0