replace属性为true时,会替换directive指向的元素;为false时将directive的内容作为子元素插入到directive指向的元素。默认为false。
看例子:
- <!DOCTYPE html>
- <html ng-app="app">
-
- <head>
- <title></title>
- <script src="//cdn.bootcss.com/angular.js/1.5.8/angular.min.js"></script>
- </head>
-
- <body>
- <div ng-controller="ctrl1">
- <div my-directive id="div1" class="my-div">
- </div>
- </div>
- <script>
- var app = angular.module('app', []);
- app.directive('myDirective', [function() {
- return {
- replace: true,
- template: '<section id="section1" class="my-section"></section>',
- scope:{},
- link: function(scope) {
- }
- }
- }])
- app.controller('ctrl1', ['$scope', function($scope) {
- }])
- </script>
- </body>
-
- </html>
div1从div换成了section。id和class合并了。
注意:replace为true时,template只能有一个根元素,否则会报错。