Search code examples
cssangularstring-interpolation

Interpolate variable into class with Angular


I would like to dynamically generate a class name for my element. For example, <span class=``icon icon-${route.title}``></span> (had to use double backticks here, but it should really be just one set of backticks.)

    <ul class="sidebar-list">
      <li *ngFor="let route of menuRoutes.items">
        <span class=`icon icon-${route.title}`></span>
        <a routerLink="/{{route.path}}" routerLinkActive="active">{{ 'menu.' + route.title | translate}}</a> 
      </li>
    </ul>


Solution

  • Try this:

    <span class="icon icon-{{route.title}}"></span>