How to pass variable to "data-target=" angular 2

TypescriptAngular

Typescript Problem Overview


I'm working with angular 2 typescript and i want to create a toggle collapse list. This is simple with single id and data-target but i loop throught a list so i have dynamic id names. I want to pass a variable in my data-target so i can reach the dynamic names of the id's. something like this:

<a data-toggle="collapse" data-target="#{{theme.themeId}}>collapseHere </a> but this gives me a syntax error.

Is it possibly to pass variables with a data-target?

Typescript Solutions


Solution 1 - Typescript

You could used attribute binding. Something like that:

[attr.data-target]="'#' + theme.themeId"

Solution 2 - Typescript

Yes exactly you can use

*ngFor="a of Array; index as i;"

and

[attr.data-target]="'#test' + i"

and

name="test{{i}}

Solution 3 - Typescript

To pass a variable in Angular 7 you can use it just like:

[attr.data-target] = "theme.themeId"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKupiView Question on Stackoverflow
Solution 1 - TypescriptThierry TemplierView Answer on Stackoverflow
Solution 2 - TypescriptMayur SanerView Answer on Stackoverflow
Solution 3 - Typescriptharshit raghavView Answer on Stackoverflow