The following is a custom component:
<app-variable-component-1></app-variable-component-1>
So my idea is as the user changes the input i.e clicks or select option then the current selected value will be stored in a property inside the parent component.
Like this:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-parent-component',
templateUrl: './parent-component.component.html',
styleUrls: ['./parent-component.component.less']
})
export class ParentComponentComponent implements OnInit {
currentOption = 1
constructor() { }
ngOnInit(): void {
}
onSelectOption(option) {
this.currentOption = option
}
}
using this property I want to change the component dynamically. I know I could have used *ngIf for each item like this:
<app-variable-component-1 *ngIf="currentOption === '1'"></app-variable-component-1>
<app-variable-component-2 *ngIf="currentOption === '2'"></app-variable-component-2>
<app-variable-component-3 *ngIf="currentOption === '3'"></app-variable-component-3>
But instead I want to shorten the code something like this:
<app-variable-component-{{currentOption}} ></app-variable-component-{{currentOption}}>
Is something similar possible in angular?