We have all used console.log at some point to debug our code. With Angular, there is an interesting alternative to display debugging information on our web page temporarily: The JSON pipe.
The primary usage is as follows:
<span>myData | json</span>Code language: HTML, XML (xml)
The above code will output your data as a JSON string in the span element, but it won’t be formatted, so the JSON string can be hard to read:
{"affiliation":"friendly","symbol":"Default Land Unit","echelon":"none","mod1":"None","mod2":"None","uniqueDesignation":"","higherFormation":"","reinforcedReduced":"","flying":false,"activity":false,"installation":false,"taskForce":false,"commandPost":"None","tacticalMissionTasks":"None","type":"Land Unit"}Code language: JavaScript (javascript)
Instead, the following syntax works a lot better:
<pre>myData | json</pre>Code language: HTML, XML (xml)
The pre HTML tag stands for “preformatted” content. As a result, that tag will preserve any whitespace, new lines, and tabs, which makes reading that JSON data a lot easier:
{
"affiliation":"friendly",
"symbol":"Default Land Unit",
"echelon":"none",
"mod1":"None",
"mod2":"None"
}Code language: JavaScript (javascript)
It’s a simple trick, yet a big time saver when debugging code that’s using complex JSON structures.