Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS leaves comments in HTML: is it possible to remove them?

Does anyone knows if you can remove the angular comments that are left in html code?

For example: If I use ngRepeat and there are no items to repeat over, AngularJS leaves this :

<!-- ngRepeat: post in posts --> 
like image 550
popo joe Avatar asked Nov 23 '13 17:11

popo joe


People also ask

How do you remove comments from HTML?

There's nothing too much to explain this feature. It does what the title says, removes every HTML comment. Everything written between the <! -- beginning and --> closing tag is considered a comment.

How do I comment out HTML in AngularJS?

You can use HTML comment syntax instead <! -- --> . The HTML commented out this way still is added to the DOM but only as comment.

Can you comment things out in HTML?

How to Write Inline Comments in HTML. You can also add comments in the middle of a sentence or line of code. Only the text inside the <! -- --> will be commented out, and the rest of the text inside the tag won't be affected.

How do I keep HTML code in comments?

To write HTML comments put <! --- and ---> at either end of the comment. HTML comments are notes to keep HTML code organized and are ignored by the browser.


2 Answers

This comment is a result of the element transclusion performed by ngRepeat. Looks like it's been happening nearly since the dawn of time (in angular terms) and will be created whenever a directive asks for element transclusion.

While you certainly could wipe it out with direct HTML manipulation, it's not a safe thing to do. Having read through the source, it suggests this comment is needed to continue compiling your HTML once the original ngRepeat element is removed. Additionally, in v1.2.0, ngRepeat adds more comments that are used to determine where repeated elements are placed.

To summarise:

  • Angular doesn't let you remove this
  • You could do it manually but you would most likely break angular
  • To reiterate the comments, it seems a strange request to remove this comment in the first place - depending on your reasoning for doing this there may be better options for what you want to achieve.
like image 83
Andyrooger Avatar answered Sep 21 '22 13:09

Andyrooger


Run this in your dev console to nuke the comments:

$("*").contents().filter(function(){ return this.nodeType == 8;}).remove(); 

Not a proper fix, but handy snippet while debugging.

like image 32
Mike Causer Avatar answered Sep 18 '22 13:09

Mike Causer