AngularJS自定义指令之可选参数replace

本文介绍了AngularJS中自定义指令的replace属性的作用。通过一个示例展示了当replace设置为true时,DOM结构将直接替换为指定的模板内容;而设置为false时,则保留原有的元素结构,仅替换内部的内容。

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

发现这个可选参数就是闲的蛋疼!!!
不多说,看代码

可选参数:replace
默认为false,可以设置为true
若设置为true,dom中显示为:

<h3>Hello World</h3>

这里写图片描述

若设置为false

<ts-hello>
    <h3>Hello World</h3>
</ts-hello>

这里写图片描述

<!DOCTYPE html>
<html ng-app='myApp'>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body ng-controller='firstCtrl'>

        <ts-hello>
            11
        </ts-hello>

        <script src='js/angular.js'></script>
        <script>
            var app = angular.module('myApp',['ng']);
            app.controller('firstCtrl',['$scope',function($scope){

            }]);
            app.directive('tsHello',function(){
                return {
                    template:'<h3>Hello World</h3>',
                    restrict:'ECAM',
                    replace:true
                }
            });
        </script>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值