angular directive详解之replace


replace属性为true时,会替换directive指向的元素;为false时将directive的内容作为子元素插入到directive指向的元素。默认为false。

看例子:

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html ng-app="app">  
  3.   
  4. <head>  
  5.   <title></title>  
  6.   <script src="//cdn.bootcss.com/angular.js/1.5.8/angular.min.js"></script>  
  7. </head>  
  8.   
  9. <body>  
  10.   <div ng-controller="ctrl1">  
  11.     <div my-directive id="div1" class="my-div">  
  12.     </div>  
  13.   </div>  
  14.   <script>  
  15.   var app = angular.module('app', []);  
  16.   app.directive('myDirective', [function() {  
  17.     return {  
  18.       replace: true,  
  19.       template: '<section id="section1" class="my-section"></section>',  
  20.       scope:{},  
  21.       link: function(scope) {  
  22.       }  
  23.     }  
  24.   }])  
  25.   app.controller('ctrl1', ['$scope', function($scope) {  
  26.   }])  
  27.   </script>  
  28. </body>  
  29.   
  30. </html>  

div1从div换成了section。id和class合并了。 注意:replace为true时,template只能有一个根元素,否则会报错。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值