简体   繁体   中英

Add css class name with a TS property Angular 7

I'm having an issue with a simple thing I guess ...

I just need my span tag to have a class named store in a variable from my .ts file :

<span [ngClass]="{'flag-icon': true, 'my_property_in_TS': true}"></span>

I tried some things like this :

<span [ngClass]="{'flag-icon': true, ${lang.codeIcon}: true}"></span>

<span [ngClass]="{'flag-icon': true, lang.codeIcon: true}"></span>

I'm sure the answer is very simple... Does someone knows how to make it ?

You could try something like this -

<span [ngClass]="['flag-icon', lang.codeIcon]"></span>

For more ways -

To use the css class stored in lang.codeIcon , do like that:

<span [ngClass]="lang.codeIcon"></span>

For many classes, use an array:

<span [ngClass]="[lang.codeIcon, 'flag-icon']"></span>

For conditionnal, use braces:

<span [ngClass]="{'flag-icon': booleanVar, lang.codeIcon: !booleanVar}"></span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM