Is ampersands (&) safe to use in AngularJS HTML templates?

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

To be clear, it would be safe to write

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

in an angular template?

I am not interested to know if it works (this is the case), but if there are any cases where it can cause problems or if it is really discouraged.

+7
source share
3 answers

It's good. The HTML5 specification explicitly allows the use of non-encoded ampersands if they are not like a character reference (e.g. &copy; ). Of course, for some things, such as URLs, it's best to be consistent and avoid them. But for && with spaces around it, there is no chance that the browser will misinterpret the data, and &amp;&amp; will be much less readable.

Relevant sections of the specification:

+3
source

I use ampersands in my Angular html templates too, and they never had a problem with them, but the best way to check whether it is safe to use them is to simply check their effect in a test application that looks like you. I think...

+2
source

Yes, that's fine, I use it all the time. Here the fiddle demonstrates:

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

http://jsfiddle.net/8drnp4ue/

0
source

All Articles