OctaviaLo
OctaviaLo

Reputation: 1396

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>

Upvotes: 1

Views: 922

Answers (2)

Yash Rami
Yash Rami

Reputation: 2327

You can also archive this by using a [ngClass] directive

<span [ngClass]="['icon', route.title]"></span>

Upvotes: 2

Try this:

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

Upvotes: 7

Related Questions