Darren Wainwright
Darren Wainwright

Reputation: 30747

Are there any disadvantages to using replace = true on an AngularJS directive?

Getting used to AngularJS and have been building quite a few directives.

Typically, where the directive is an Element I have been setting the replace option to true

I wondered though, does doing this have any negative impact on anything? I'm setting it to true so that it just keeps the source html nice and tidy, and as close to html specs as possible.

However, if it's likely that setting this to true has some negative impact I'd forego the specs...

So - are there advantages/disadvantages to replace:true ?

Upvotes: 0

Views: 194

Answers (2)

sethro
sethro

Reputation: 2127

One thing to keep in mind when using replace: true is that your template can only have one root node. So, if you want to have a template with several siblings at its root level, use replace: false and your template will be inserted into the inner HTML of the original element instead of replacing it, completely.

This rule is enforced because angular tries to transfer all attributes of the original element to the template's root node.

With that in mind, I think it's a stylistic choice: What makes most sense, semantically, for your application?

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245459

There are no negative impacts, but there is a specific use case for setting replace to true.

If you want to replace whatever markup may be in the element already with your own markup, set it to true.

If you want to live in harmony with whatever markup is already in the directive, set it to false.

Upvotes: 1

Related Questions