I am facing an issue where a function is passing the name of my component as a string, but I actually need it to be the actual component class:
Currently: "MyComponent" Required: MyComponent
I need to find a way to properly convert it. In my code, there is an ngFor
loop that iterates over values from a dashboard array, and I am attempting to use a pipe to handle this conversion:
<div id="grid">
<gridster [options]="options">
<gridster-item [item]="item" *ngFor="let item of dashboard; let i = index;">
<div class="grid-header drag-handle">
<span class="handle-icon"><i class="material-icons">open_with</i></span>
<span class="close-icon" (click)="removePanel(item)"><i class="material-icons">close</i></span>
</div>
<div class="grid-content">
<ndc-dynamic [ndcDynamicComponent]="item.viewType | dynamicComponent"></ndc-dynamic>
</div>
</gridster-item>
</gridster>
</div>
In the code snippet above, you can see that I have a property called item.viewType
which is a string coming from my item array. I am passing this value to a custom pipe called dynamicComponent
. I have imported all my components into my dynamic-component.pipe.ts
file. The challenge is to transform the string into the specified viewType
and return the typed value:
import { Pipe, PipeTransform } from '@angular/core';
// Importing all possible components
import * from '../../views';
@Pipe({
name: 'dynamicComponent'
})
export class DynamicComponentPipe implements PipeTransform {
transform(value: string): any {
}
}