Javascript 有没有什么办法可以用AngularJS写注释,这样在查看源码时就看不到了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11334335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way to write comments with AngularJS so that they are not visible when viewing the source
提问by dnc253
When writing JSPs, I always use <%-- --%>
instead of <!-- -->
to write any comments, since it will not output to the HTML. I have begun using AngularJS, and I'm wondering if there's any similar kind of comment construct that I could use. It's not absolutely necessary to not have the comments visible in the HTML source, but I much rather them not being there.
在编写 JSP 时,我总是使用<%-- --%>
而不是<!-- -->
编写任何注释,因为它不会输出到 HTML。我已经开始使用AngularJS,我想知道是否有任何类似的注释结构可以使用。在 HTML 源代码中不显示注释并不是绝对必要的,但我宁愿它们不在那里。
With the limited knowledge I have of AngularJS, I'm not sure how they could prevent it from being in the source, but I just want to ask in case there is a way. Perhaps there's also a best practice for comments in AngularJS? Any input would be appreciated.
由于我对 AngularJS 的了解有限,我不确定他们如何防止它出现在源代码中,但我只是想问问有没有办法。也许在 AngularJS 中也有评论的最佳实践?任何输入将不胜感激。
采纳答案by Simeon Visser
AngularJS is a library for dynamic features in HTML/JS/CSS pages. It's not a template language such as JavaServer Pages. As such, it doesn't have a special syntax for comments. You can still use HTML style comments of course.
AngularJS 是一个用于 HTML/JS/CSS 页面动态特性的库。它不是像 JavaServer Pages 这样的模板语言。因此,它没有用于注释的特殊语法。当然,您仍然可以使用 HTML 样式的注释。
回答by M M
You should use a pre-deployment processing tool such as grunt uglify. It will allow you to strip out comments and do more useful deployment pre-processing such as minifying code.
您应该使用诸如 grunt uglify 之类的预部署处理工具。它将允许您删除注释并进行更有用的部署预处理,例如缩小代码。
回答by user1636608
Use directives:
使用指令:
myApp.directive('comment', ['$rootScope', function($rootScope) {
return {
restrict: 'AEC',
link: function($scope, element, attrs){
$(element).remove();
}
};
}]);
example:
例子:
<comment>Remove me</comment>