How to use `replace` of directive definition?

AngularjsAngularjs Directive

Angularjs Problem Overview


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

Angularjs Solutions


Solution 1 - Angularjs

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.

Solution 2 - Angularjs

As the documentation states, 'replace' determines whether the current element is replaced by the directive. The other option is whether it is just added to as a child basically. If you look at the source of your plnkr, notice that for the second directive where replace is false that the div tag is still there. For the first directive it is not.

First result:

<span myd1="">directive template1</span>

Second result:

<div myd2=""><span>directive template2</span></div>

Solution 3 - Angularjs

Replace [True | False (default)]

> Effect

1.	Replace the directive element. 

> Dependency:

1. When replace: true, the template or templateUrl must be required. 

Solution 4 - Angularjs

Also i got this error if i had the comment in tn top level of template among with the actual root element.

<!-- Just a commented out stuff -->
<div>test of {{value}}</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFreewindView Question on Stackoverflow
Solution 1 - AngularjscheckettsView Answer on Stackoverflow
Solution 2 - AngularjsRyan O'NeillView Answer on Stackoverflow
Solution 3 - Angularjsuser3454062View Answer on Stackoverflow
Solution 4 - AngularjsKonstantin IsaevView Answer on Stackoverflow