Utilizing ResolveComponentFactory() with a String Key: A Step-by-Step Guide

My goal:

  • I want to find a way to use something similar to the "resolveComponentFactory()", but with a 'string' identifier to obtain Component Factories.
  • Once I have them, I plan to utilize the "createComponent(Factory)" method.

Check out this Plnkr Example -> here

In the example code, you will come across the AddItem method

addItem(componentName:string):void{
    let compFactory: ComponentFactory;

    switch(componentName){
        case "image":
            compFactory = this.compFactoryResolver.resolveComponentFactory(PictureBoxWidget);
            break;
        case "text":
            compFactory = this.compFactoryResolver.resolveComponentFactory(TextBoxWidget);
            break;
    }
    
    //I am looking for a way to dynamically resolve a component based on a string
    //without having to hard code all possible options
    
    this.container.createComponent(compFactory);
}

The "compFactoryResolver:ComponentFactoryResolver" is injected in the constructor.

While using a switch statement works, it's not the most efficient solution and can lead to maintenance issues.

Inspecting the ComponentFactoryResolver in the console, I noticed that it contains a Map with different factories.

CodegenComponentFactoryResolver {_parent: AppModuleInjector, _factories: Map}

However, accessing this map directly seems challenging as it is private.

Is there a better approach than creating my own class to access this factory list?

I've seen many discussions on dynamic component creation, but here I am specifically trying to retrieve factories using strings as keys.

Any suggestions or guidance would be greatly appreciated.

Thank you for your help.

Answer №1

In one scenario, a map of available components is being defined,

const compMap = {
  text: PictureBoxWidget,
  image: TextBoxWidget
};

Alternatively, identifiers are being defined as static class properties to generate a map,

const compMap = [PictureBoxWidget, TextBoxWidget]
.map(widget => [widget.id, widget])
.reduce((widgets, [id, widget]) => Object.assign(widgets, { [id]: widget }), {});

This map is then used in the following way:

let compFactory: ComponentFactory;

if (componentName in compMap) {
    compFactory = this.compFactoryResolver.resolveComponentFactory(compMap[componentName]);
} else {
    throw new Error(`Unknown ${componentName} component`);
}

It's important to note that component classes cannot be identified as strings automatically, since Angular 2 DI does not resolve them into strings like it did in Angular 1.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Execute automated tests using Selenium on an Angular web application using headless Chrome

I am currently working on developing Selenium tests for an Angular application. My objective is to have these tests integrated into my continuous integration builds, requiring them to be executed in headless mode using Chrome. So far, I have created two s ...

Utilizing Angular to parse a JSON string generated with json.dumps

I am having trouble finding a solution that works for me. Currently, I am using python 3.6 (Django Rest Framework) on the server side and Angular 5 on the client side. This is the code on the server: class TypesView(APIView): def get(self,request): ...

Using TypeScript to Load JSON Data from a Folder

I'm a newcomer to TypeScript and encountering an issue. My task involves dynamically loading objects from a directory that houses multiple JSON files. The file names are generated through an export bash script populating the resource directory. I wan ...

Is ngOnDestroy in Angular 2 triggered on page refresh, or only when navigating away from the component?

Can anyone clarify whether the ngOnDestroy method in Angular 2 is triggered on page refresh or only when navigating away? ...

Developing an Angular project may result in an overflow of memory

After attempting to construct an Angular project on a Linux server with the -aot flag, I encountered an error. Despite upgrading my instance to a higher CPU core, increasing RAM, and enabling a SWAP partition, the error persisted and even occurred more rap ...

Factory function in Angular for translating using arrow syntax

When I include TranslateModule using the following code: TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: HttpLoaderFactory, deps: [HttpClient] } }) where export function HttpLoaderFactory(http: H ...

Stop angular material css styles from affecting neighboring components

Currently, I have overridden the angular material style in my global SCSS style as follows: .mat-form-field-infix{ padding: 0.5em 0 0.5em 0 !important; } However, I now need to apply a different padding to the same element in my component for addition ...

Transitioning React components organized in groups to TypeScript

As I transition my react project to incorporate typescript, one challenge I encountered was adjusting the file structure. In its simplified form, here is how the original js project's file structure looked like: src components index.js inputs butt ...

Replicating entities in TypeScript

I am currently developing an Angular 2 application using TypeScript. In a User Management component, I have implemented a table that displays all the users in my system. When a user is clicked on within the table, a form appears with their complete set of ...

Creating a Bottom Sliding Menu in Ionic 2

Hey there! I'm working on something that might not fit the typical definition of a sliding menu. It's not for navigation purposes, but rather to house some data. My idea for this actually comes from Apple Maps: https://i.stack.imgur.com/hL1RU.jp ...

Tips for transferring numerous photos from Angular 8 to ASP .NET Core

I have been struggling to find a solution for the issue I am facing. When sending multiple images from Angular using "from data", although the files are successfully sent in the network request, the parameters on the API method show count=0. Here is the s ...

Retrieve a particular element from an array within a JSON object using Ionic

I am currently facing a challenge in extracting a specific array element from a JSON response that I have retrieved. While I can successfully fetch the entire feed, I am struggling to narrow it down to just one particular element. Here is what my service ...

How to trigger a click event in React using TypeScript and material-ui library

Currently, I am facing an issue when trying to update the value of material-ui TextFields from the store. When manually typing inside the field, everything works fine as expected with the handleChange() and handleBlur() functions handling the events. Howev ...

When attempting to access Element.object3D in A-frame using TypeScript, an error stating "The 'object3D' property does not exist on the 'Element' type" is encountered

As a Japanese student, I ask for your understanding regarding my limited English proficiency. Currently, I am working on developing an a-frame library that utilizes anime.js in TypeScript to create animations. However, I encountered an issue when attemptin ...

Retrieve server information without utilizing data.map since array.map is not a supported function in next.js version 13

Currently, I am developing a list page using next.js 13 to display a collection of projects. I made an attempt to enable server-side rendering for this feature. The implementation is located in app/team/page.tsx import { useRouter } from 'next/navig ...

The process of deploying production-ready code using webpack and angular2

Currently, my Angular 2 code utilizes both webpack and grunt. During development, I rely on webpack-dev-server as a grunt task. When it comes to preparing the code for production deployment, I handle all minification tasks through Grunt which results in t ...

Encountering a failure in library construction while using Angular 9

Currently, I am in the process of updating this library https://github.com/flauc/angular2-notifications from Angular 2+ to Angular 9. The initial error was related to the ModuleWithProviders becoming a generic type, which I have successfully addressed. Ad ...

Using the pipe operator in RXJS to transform an Event into a KeyboardEvent

I'm encountering an error when trying to add a keydown event and convert the parameter type from Event to KeyboardEvent as shown below. fromEvent(document, "keydown") .pipe<KeyboardEvent, KeyboardEvent>( filter((event) => even ...

Type of Data for Material UI's Selection Component

In my code, I am utilizing Material UI's Select component, which functions as a drop-down menu. Here is an example of how I am using it: const [criteria, setCriteria] = useState(''); ... let ShowUsers = () => { console.log('Wor ...

"Simply click and drag the preselected item into the viewer to add it

I am looking to create an angular page with specific functionality: Enable users to upload a document (no problem) Display the document's content Allow users to drag a predetermined object onto the document display area to indicate where they will si ...