Search code examples
angularng-class

ngClass of undefined


I have the following

<h6 *ngSwitchCase="'text'" class="custom-popup-content-text">{{content.value}}</h6>

In my code, I pass sometime also a content.className, and want to add that class if the className is defined

I tried

<h6 *ngSwitchCase="'text'" [ngClass]="['custom-popup-content-text', content.className]">{{content.value}}</h6>

but this one doesn't work if classname is undefined

I tried also

<h6 *ngSwitchCase="'text'" [ngClass]="{'custom-popup-content-text' : true, content.className : content.className ? 'content.className' : ''}">{{content.value}}</h6>

but I got: Missing expected : at column 45 in

[{'custom-popup-content-text' : true, content.className : content.className ? content.className : ''}]

What I might be wrong?


Solution

  • try the following :

    <h6 *ngSwitchCase="'text'" [ngClass]="['custom-popup-content-text', ((content.className) ? content.className : '')]">{{content.value}}</h6>