Angularjs[25] - 自定义指令(1)(restrict, template, replace 属性)

本文详细介绍了Angular中自定义指令的方法,包括不同限制类型(E/C/A/M)的应用场景及示例,同时提供了如何设置template及replace参数的具体指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义指令:

  1. module.directive(name,directiveFactory)
  2. @see $compileProvider.directive()

⚠️  不要使用 ng 为指令,若指令名为 xxx-yyy,在设置指令名时应为 xxxYyy 即驼峰命名法。

  • restrict: 可以任意组合四种风格,如果忽略 restrict,默认为A。
字母风格示例
E元素<my-dir></my-dir>
C样式类<span class="my-dir:exp;"></span>
A属性<span my-dir="exp"></span>
M注释<!-- directive: my-dir:exp-->

 

  • template: 模板内容,这个内容根据 replace 参数设置替换节点或仅替换节点内容。
  • replace: 若此配置为 true 则替换指令所在元素,若为 false 或不指定,则把当前指令追加到所在元素内。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div ng-app="myApp">
        <custom-tags>222</custom-tags>
        <div class="custom-tags"></div>
        <div custom-tags></div>
        <!-- directive: custom-tags -->
    </div>

<script type="text/javascript" src="../../vendor/angular/angularjs.js"></script>
<script type="text/javascript" src="app/index.js"></script>
</body>
</html>
var myApp = angular.module('myApp',[],['$compileProvider',function ($compileProvider) {
    $compileProvider.directive('customTags',function () {
        return{
            restrict: 'ECAM',
            template:'<div>custom-tags-html</div>',
            replace: true
        }
    });
}])

 

转载于:https://www.cnblogs.com/bky-1083/p/6358997.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值