skip to content
Alvin Lucillo

Conditionally show SVG

/ 1 min read

💻 Tech

If you want to conditionally show an SVG code, you can first use the @if directive. But first, let’s say you want to find SVG code. If you’re using Material Design, you can go to Google Fonts and search for an icon. For example, I searched for task and downloaded the SVG file. You can open the SVG file with a text editor.

Task SVG code:

<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#e8eaed"><g><path d="M0,0h24v24H0V0z" fill="none"/></g><g><path d="M14,2H6C4.9,2,4.01,2.9,4.01,4L4,20c0,1.1,0.89,2,1.99,2H18c1.1,0,2-0.9,2-2V8L14,2z M18,20H6V4h7v5h5V20z M8.82,13.05 L7.4,14.46L10.94,18l5.66-5.66l-1.41-1.41l-4.24,4.24L8.82,13.05z"/></g></svg>

Now, with Angular 16, I conditionally show it in an HTML file:

<div>
  @if (taskType === 'Task') {
<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#e8eaed">
   <g>
      <path d="M0,0h24v24H0V0z" fill="none"/>
   </g>
   <g>
      <path d="M14,2H6C4.9,2,4.01,2.9,4.01,4L4,20c0,1.1,0.89,2,1.99,2H18c1.1,0,2-0.9,2-2V8L14,2z M18,20H6V4h7v5h5V20z M8.82,13.05 L7.4,14.46L10.94,18l5.66-5.66l-1.41-1.41l-4.24,4.24L8.82,13.05z"/>
   </g>
</svg>