Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a comment (for developers, not in output HTML) to an Angular template?

I'm used to the more popular 'mustache' style templates where I can add a comment for my colleagues with:

{# The following code looks a bit odd, but here's why... #} 

These comments obviously don't appear in the output - so users don't see them. How can I do something similar in Angular?

like image 577
mikemaccana Avatar asked Aug 05 '13 16:08

mikemaccana


People also ask

How do you comment out Angular codes?

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

Does Angular remove HTML comments?

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.

How do I comment in AngularJS?

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.

Which of the following HTML element is valid in Angular template?

Almost all HTML syntax is valid template syntax. However, because an Angular template is part of an overall webpage, and not the entire page, you don't need to include elements such as <html> , <body> , or <base> , and can focus exclusively on the part of the page you are developing.


1 Answers

Angular doesn't have template comment support built in. You could, however, create a comment directive to support it, like this.

app.directive('templateComment', function () {     return {         restrict: 'E',         compile: function (tElement, attrs) {             tElement.remove();         }     }; }); 

Markup would then be:

<template-comment>Put your comment here.</template-comment> 

Alternately, you could use standard html comments, and then strip them out of your production code before deployment.

Consider this grunt task, if you'd like to support block comments - https://github.com/philipwalton/grunt-strip-code Specify a start comment and an end comment, and your comment block will be stripped out of the production code, assuming your add this task to your deploy target. Use it as a model for you build process, if you're not using Grunt. ....

like image 104
Pauli Price Avatar answered Oct 05 '22 23:10

Pauli Price