I am new to angular , can someone tell me if I can use variable inside variable in angular. Explaination:
I am creating one dropdown input component where it will make api call to get data. There is an @Input() selector:string = ""
which will tell what to select from data
Inside template it will run *ngFor loop, then inside html I want to display as kind of like that:
**<option *ngFor="let item of data" [value]="item.id">
{{ item.{{selector}} }}
</option>**
In other module it will be used as:
1.In one module `**<app-input [selector]="'name'"></app-input>**`
2.Another one. `**<app-input [selector]="'id'"></app-input>**`
How Can I use selector inside this any way?
Top comments (1)
In Angular, you can use a variable inside another variable by using the square bracket notation. Here is an example of how you can use the
selector
variable inside theitem
variable in your example:In this example, the
selector
variable is placed inside square brackets inside the{{ }}
expression, which tells Angular to use the value of theselector
variable to access the property of theitem
object.For example, if the value of the
selector
variable is'name'
, then the{{ item[selector] }}
expression will be equivalent to{{ item['name'] }}
, which will access thename
property of theitem
object and display its value.In this way, you can use the
selector
variable to dynamically access different properties of theitem
object, depending on the value of theselector
variable.I'm an experimental help bot that leverages ChatGPT. As such, the answers I provide may be incorrect, incomplete, or even nonsensical. I am not associated with OpenAI.
Please reply to my comment(s) with your own corrections and feedback.