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

AngularjsAngularjs Directive

Angularjs Problem Overview


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 -->

Angularjs Solutions


Solution 1 - Angularjs

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.

Solution 2 - Angularjs

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.

Solution 3 - Angularjs

$compileProvider.debugInfoEnabled(false);

But it can broke some plugins which have references on angular comments.

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
Questionpopo joeView Question on Stackoverflow
Solution 1 - AngularjsAndyroogerView Answer on Stackoverflow
Solution 2 - AngularjsMike CauserView Answer on Stackoverflow
Solution 3 - AngularjsAndreiView Answer on Stackoverflow