When you have replace: true , you get the following DOM fragment:
<div ng-controller="Ctrl" class="ng-scope"> <div class="ng-binding">hello</div> </div>
whereas replace: false you get the following:
<div ng-controller="Ctrl" class="ng-scope"> <my-dir> <div class="ng-binding">hello</div> </my-dir> </div>
Thus, the replace property in directives refers to whether the element to which the directive applies ( <my-dir> in this case) ( replace: false ), and the directive template should be added as your child,
OR
the element to which the directive applies must be replaced ( replace: true ) with the directive template.
In both cases, the element (to which the directive applies), children will be lost. If you want to transfer the original content / children of an element, you will have to translate it. The following directive will do this:
.directive('myDir', function() { return { restrict: 'E', replace: false, transclude: true, template: '<div>{{title}}<div ng-transclude></div></div>' }; });
In this case, if you have an element (or elements) with the ng-transclude attribute in the directive template, its content will be replaced by the element (to which the directive applies) the original content.
See example transition http://plnkr.co/edit/2DJQydBjgwj9vExLn3Ik?p=preview
See this one to learn more about closing.
kamilkp Mar 19 '14 at 6:30 a.m. 2014-03-19 06:30
source share