Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use ampersands (&) in AngularJS html templates

I have been told that using ampersands in angular templates is bad practice since '&' is reserved in HTML, but I see it in examples for angular all the time.

To be clear, would it be safe to write

<div ng-show="bool1 && bool2"></div>

in an angular template?

I'm not interested in knowing if it works (it does), but if there are any edge cases where this could cause problems or if it's in fact discouraged.

like image 546
Jacob Sievers Avatar asked Sep 03 '15 13:09

Jacob Sievers


People also ask

Are ampersands professional?

Ampersands in business writing Answer: You can use ampersands in titles, signage and website buttons where space is limited or the ampersand is part of an organisation's branding. Use and, not ampersands in business writing, even for emails. It is more professional.

Why are ampersands so popular?

That's because ampersands are usually only used for display type–the large or eye-catching type used in headlines, signage, and advertisements–so the character can be more ornate, even in fonts that are primarily designed for text.

Is it informal to use an ampersand?

Although ampersands are thought of as informal, if the ampersand is officially part of a company name, it's best to use the ampersand instead of writing out the word “and.” For example, you write “Tiffany & Co.,” “Procter & Gamble,” and “AT&T” with ampersands.

Can you use ampersand in legal documents?

Ampersands are completely correct when they're part of a proper name ( Fromage & Cracotte LLP ) or an official citation format ( Bus. & Prof. Code § 17200 ). Otherwise, ampersands should be handled like any other contraction: the more formal the document, the less they should be used.


2 Answers

It's fine. The HTML5 spec explicitly allows unencoded ampersands if they don't look like a character reference (such as &copy;). Sure, for some things like URLs it's better to be consistent and escape them. But for && with spaces around it, there's no chance that the browser will misinterpret the data, and &amp;&amp; is significantly less readable.

Relevant Sections of the spec:

  • https://www.w3.org/TR/html5/syntax.html#escapable-raw-text-elements
  • https://www.w3.org/TR/html5/syntax.html#syntax-ambiguous-ampersand
like image 146
rjh Avatar answered Sep 21 '22 07:09

rjh


I use ampersands in my Angular html templates too and never had a problem with them, but the best way to see if it's safe for you to use them is to just test their effect in a test app that resembles yours I guess...

like image 33
FullyHumanProgrammer Avatar answered Sep 23 '22 07:09

FullyHumanProgrammer