In this document: http://docs.angularjs.org/guide/directive , it says that there is a replace
configuration for directives:
template - replace the current element with the contents of the HTML. The replacement process migrates all of the attributes / classes from the old element to the new one. See the Creating Components section below for more information.
javascript code
app.directive('myd1', function(){ return { template: '<span>directive template1</span>', replace: true } }); app.directive('myd2', function(){ return { template: '<span>directive template2</span>', replace: false } });
html code
<div myd1> original content should be replaced </div> <div myd2> original content should NOT be replaced </div>
But the final page is looking like:
directive template1 directive template2
It seems the replace
doesn't work. Do I miss anything?
Live demo: http://plnkr.co/edit/rGIgmjO81X2UxJohL4HM?p=preview
In this document: http://docs.angularjs.org/guide/directive , it says that there is a replace configuration for directives: template - replace the current element with the contents of the HTML. The replacement process migrates all of the attributes / classes from the old element to the new one.
AngularJS Directive's replace option can be used to replace the container element itself by directive content. By default, the directive content inserted as the child of the element directive is applied on. But using replace, that container element altogether can be replaced by directive's actual content HTML.
At the core, a directive is a function that executes whenever the Angular compiler finds it in the DOM. Angular directives are used to extend the power of the HTML by giving it new syntax. Each directive has a name — either one from the Angular predefined like ng-repeat , or a custom one which can be called anything.
You are getting confused with transclude: true
, which would append the inner content.
replace: true
means that the content of the directive template will replace the element that the directive is declared on, in this case the <div myd1>
tag.
http://plnkr.co/edit/k9qSx15fhSZRMwgAIMP4?p=preview
For example without replace:true
<div myd1><span class="replaced" myd1="">directive template1</span></div>
and with replace:true
<span class="replaced" myd1="">directive template1</span>
As you can see in the latter example, the div tag is indeed replaced.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With