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

僤鯓⒐⒋嵵緔 提交于 2019-12-17 07:25:07

问题


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

回答1:


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.



回答2:


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.




回答3:


$compileProvider.debugInfoEnabled(false);

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



来源:https://stackoverflow.com/questions/20165390/angularjs-leaves-comments-in-html-is-it-possible-to-remove-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!